newman-reporter-reportportal
Advanced tools
Comparing version 0.0.16 to 0.0.17
225
lib/index.js
'use strict'; | ||
const RPClient = require('reportportal-client'), | ||
StringDecoder = require('string_decoder').StringDecoder, | ||
_ = require('lodash'), | ||
utils = require('./utils'), | ||
/** | ||
* Basic error handler for promises. Just prints errors. | ||
* | ||
* @param {Object} err Promise's error | ||
*/ | ||
errorHandler = err => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
}, | ||
/** | ||
* Possible test execution statuses. | ||
@@ -26,6 +34,3 @@ * | ||
*/ | ||
TEST_PATTERNS = [ | ||
/[\s]*pm.test\("(.*?)"/, | ||
/[\s]*tests\["(.*?)"\]/ | ||
]; | ||
TEST_PATTERNS = [/[\s]*pm.test\("(.*?)",/, /[\s]*tests\["(.*?)"\]/]; | ||
@@ -50,3 +55,3 @@ /** | ||
/** | ||
* Basic launch object for the current test run. | ||
* Basic launch object of the current test run. | ||
* | ||
@@ -60,3 +65,7 @@ * @type {Object} | ||
// Starts a new launch | ||
emitter.on('start', () => { | ||
emitter.on('start', err => { | ||
if (err) { | ||
throw err; | ||
} | ||
let description = 'Newman launch'; | ||
@@ -69,4 +78,7 @@ | ||
launchObj = client.startLaunch({ | ||
description: description | ||
description: description, | ||
tags: getTags(collectionRunOptions.collection.variables) | ||
}); | ||
launchObj.promise.catch(errorHandler); | ||
}); | ||
@@ -82,5 +94,9 @@ | ||
name: o.item.name, | ||
type: 'TEST' | ||
}, launchObj.tempId); | ||
type: 'TEST', | ||
tags: getTags(collectionRunOptions.collection.variables) | ||
}, | ||
launchObj.tempId); | ||
testObj.promise.catch(errorHandler); | ||
collectionMap.set(o.cursor.ref, { | ||
@@ -98,9 +114,13 @@ testId: testObj.tempId, | ||
const testObj = collectionMap.get(o.cursor.ref); | ||
const stepName = o.request.description ? o.request.description.content : 'Postman request'; | ||
const testObj = collectionMap.get(o.cursor.ref), | ||
stepName = o.request.description ? o.request.description.content : 'Postman request', | ||
stepObj = client.startTestItem({ | ||
name: stepName, | ||
type: 'STEP', | ||
tags: getTags(collectionRunOptions.environment.values) | ||
}, | ||
launchObj.tempId, | ||
testObj.testId); | ||
const stepObj = client.startTestItem({ | ||
name: stepName, | ||
type: 'STEP' | ||
}, launchObj.tempId, testObj.testId); | ||
stepObj.promise.catch(errorHandler); | ||
@@ -114,3 +134,2 @@ // Sends request's metadata | ||
name: stepName | ||
}); | ||
@@ -125,14 +144,19 @@ }); | ||
_.filter(o.events, event => event.script) | ||
const testObj = collectionMap.get(o.cursor.ref); | ||
_.filter(o.events, 'script') | ||
.flatMap(event => event.script.exec) // Extracts test script's strings | ||
.flatMap(exec => getStepNames(exec)) // Extracts test names from script's strings | ||
.forEach(stepName => { | ||
const testObj = collectionMap.get(o.cursor.ref); | ||
// Starts a new step for every test in a test script | ||
const stepObj = client.startTestItem({ | ||
name: stepName, | ||
type: 'STEP' | ||
}, launchObj.tempId, testObj.testId); | ||
type: 'STEP', | ||
tags: getTags(collectionRunOptions.environment.values) | ||
}, | ||
launchObj.tempId, | ||
testObj.testId); | ||
stepObj.promise.catch(errorHandler); | ||
testObj.steps.push({ | ||
@@ -154,6 +178,6 @@ stepId: stepObj.tempId, | ||
collectionMap.forEach((testObj, ref) => { | ||
for (let [ref, testObj] of collectionMap) { | ||
finishSteps(ref, testObj, run); | ||
finishTest(ref, testObj, run); | ||
}); | ||
} | ||
}); | ||
@@ -167,18 +191,23 @@ | ||
client.finishLaunch(launchObj.tempId, { | ||
status: o.run.failures ? TestStatus.FAILED : TestStatus.PASSED | ||
}); | ||
client | ||
.finishLaunch(launchObj.tempId, { | ||
status: o.run.failures ? TestStatus.FAILED : TestStatus.PASSED | ||
}) | ||
.promise.catch(errorHandler); | ||
}); | ||
/** | ||
* Constructs log message body for RP client. | ||
* Sends log message to RP using it's client. | ||
* | ||
* @param {string} id RP's item id. | ||
* @param {string} value Message value. | ||
* @param {string} [level="INFO"] Log level. | ||
*/ | ||
function logMessage(value, level = 'INFO') { | ||
return { | ||
level: level, | ||
message: value | ||
}; | ||
function logMessage (id, value, level = 'INFO') { | ||
client | ||
.sendLog(id, { | ||
level: level, | ||
message: value | ||
}) | ||
.promise.catch(errorHandler); | ||
} | ||
@@ -192,13 +221,14 @@ | ||
*/ | ||
function sendRequestLogs(stepId, request) { | ||
client.sendLog(stepId, logMessage(`Request: URL: ${request.url.toString()}`)); | ||
client.sendLog(stepId, logMessage(`Request: Method: ${request.method}`)); | ||
function sendRequestLogs (stepId, request) { | ||
logMessage(stepId, `Request: URL: ${request.url.toString()}`); | ||
logMessage(stepId, `Request: Method: ${request.method}`); | ||
const headers = request.headers.members.map(header => `${header.key}:${header.value}`); | ||
if (headers.length) { | ||
client.sendLog(stepId, logMessage(`Request: Headers: ${headers}`)); | ||
logMessage(stepId, `Request: Headers: ${headers}`); | ||
} | ||
if (request.body && request.body.toString()) { | ||
client.sendLog(stepId, logMessage(`Request: Body: ${request.body.toString()}`)); | ||
logMessage(stepId, `Request: Body: ${request.body.toString()}`); | ||
} | ||
@@ -213,11 +243,9 @@ } | ||
*/ | ||
function sendResponseLogs(stepId, response) { | ||
client.sendLog(stepId, logMessage(`Response: Code: ${response.code}`)); | ||
client.sendLog(stepId, logMessage(`Response: Status: ${response.status}`)); | ||
function sendResponseLogs (stepId, response) { | ||
const headers = response.headers.members.map(header => `${header.key}:${header.value}`); | ||
client.sendLog(stepId, logMessage(`Response: Headers: ${headers}`)); | ||
const decoder = new StringDecoder('utf8'); | ||
client.sendLog(stepId, logMessage(`Response: Body: ${decoder.write(response.stream)}`)); | ||
logMessage(stepId, `Response: Code: ${response.code}`); | ||
logMessage(stepId, `Response: Status: ${response.status}`); | ||
logMessage(stepId, `Response: Headers: ${headers}`); | ||
logMessage(stepId, `Response: Body: ${new StringDecoder('utf8').write(response.stream)}`); | ||
} | ||
@@ -228,10 +256,12 @@ | ||
* | ||
* @param {string} ref Reference id to the current test run. | ||
* @param {Object} testObj Test item which steps have to be finished. | ||
* @param {Array} failures Array of failures which were happened during test run. | ||
* @param {string} reference Reference id to the current test run. | ||
* @param {Object} testObj Test item steps of which have to be finished. | ||
* @param {Object} run Object with iformation about current test run. | ||
*/ | ||
function finishSteps(ref, testObj, run) { | ||
function finishSteps (reference, testObj, run) { | ||
// First of all check request step | ||
const requestStep = testObj.steps.find(step => step.requestId); | ||
const execution = run.executions.find(exec => exec.id == requestStep.requestId); | ||
const requestStep = _.find(testObj.steps, 'requestId'), | ||
execution = _.find(run.executions, { | ||
id: requestStep.requestId | ||
}); | ||
@@ -241,2 +271,3 @@ if (execution.requestError) { | ||
failAllSteps(testObj, execution.requestError.message); | ||
return; | ||
@@ -248,9 +279,13 @@ } | ||
// Check for script failures | ||
const scriptFailure = _.find(run.failures, failure => | ||
failure.at == 'test-script' && | ||
failure.cursor.ref == ref); | ||
const scriptFailure = _.find(run.failures, { | ||
at: 'test-script', | ||
cursor: { | ||
ref: reference | ||
} | ||
}); | ||
if (scriptFailure) { | ||
//Fails all steps with the same error if there is an error in a test-script | ||
// Fails all steps with the same error if there is an error in a test-script | ||
failAllSteps(testObj, scriptFailure.error.message); | ||
return; | ||
@@ -260,15 +295,22 @@ } | ||
// Otherwise finishes all steps according to their results | ||
testObj.steps.forEach(step => { | ||
const failure = _.find(run.failures, failure => | ||
failure.cursor.ref == ref && | ||
failure.error.test == step.name); | ||
_.forEach(testObj.steps, step => { | ||
const failure = _.find(run.failures, { | ||
cursor: { | ||
ref: reference | ||
}, | ||
error: { | ||
test: step.name | ||
} | ||
}); | ||
if (failure) { | ||
// Log error message for failed steps | ||
client.sendLog(step.stepId, logMessage(failure.error.message, 'ERROR')); | ||
logMessage(step.stepId, failure.error.message, 'ERROR'); | ||
} | ||
client.finishTestItem(step.stepId, { | ||
status: failure ? TestStatus.FAILED : TestStatus.PASSED | ||
}); | ||
client | ||
.finishTestItem(step.stepId, { | ||
status: failure ? TestStatus.FAILED : TestStatus.PASSED | ||
}) | ||
.promise.catch(errorHandler); | ||
}); | ||
@@ -282,8 +324,11 @@ | ||
*/ | ||
function failAllSteps(testObj, message) { | ||
testObj.steps.forEach(step => { | ||
client.sendLog(step.stepId, logMessage(message, 'ERROR')); | ||
client.finishTestItem(step.stepId, { | ||
status: TestStatus.FAILED | ||
}); | ||
function failAllSteps (testObj, message) { | ||
_.forEach(testObj.steps, step => { | ||
logMessage(step.stepId, message, 'ERROR'); | ||
client | ||
.finishTestItem(step.stepId, { | ||
status: TestStatus.FAILED | ||
}) | ||
.promise.catch(errorHandler); | ||
}); | ||
@@ -296,12 +341,14 @@ } | ||
* | ||
* @param {string} ref Reference id to the current test run. | ||
* @param {string} reference Reference id to the current test run. | ||
* @param {Object} testObj Test item to finish. | ||
* @param {Array} failures Array of failures which were happened during test run. | ||
* @param {Object} run Object with information about current test run. | ||
*/ | ||
function finishTest(ref, testObj, run) { | ||
const failed = _.some(run.failures, failure => failure.cursor.ref == ref); | ||
function finishTest (reference, testObj, run) { | ||
const failed = _.some(run.failures, ['cursor.ref', reference]); | ||
client.finishTestItem(testObj.testId, { | ||
status: failed ? TestStatus.FAILED : TestStatus.PASSED | ||
}); | ||
client | ||
.finishTestItem(testObj.testId, { | ||
status: failed ? TestStatus.FAILED : TestStatus.PASSED | ||
}) | ||
.promise.catch(errorHandler); | ||
} | ||
@@ -314,6 +361,7 @@ | ||
* @param {string} script Newman's test script string. | ||
* @returns {Array} Array of step names from given script string. | ||
* @returns {Array} Array of step names from the given script's string. | ||
*/ | ||
function getStepNames(script) { | ||
return script.split(';') | ||
function getStepNames (script) { | ||
return script | ||
.split(';') | ||
.sliceOn(0, x => x.includes('//')) // Removes commented elements | ||
@@ -323,2 +371,17 @@ .map(x => utils.matchPattern(x, TEST_PATTERNS, 1)) | ||
} | ||
/** | ||
* Extracts a value of the 'tags' variable from the given array and transforms it's value to array | ||
* of tags. | ||
* | ||
* @param {Object} variables Object that cintains array of postman's variables. | ||
* @returns {Array} Array of tags. | ||
*/ | ||
function getTags (variables) { | ||
const tags = _.find(variables.members, { | ||
key: 'tags' | ||
}); | ||
return tags ? tags.value.split(';') : []; | ||
} | ||
}; |
@@ -6,3 +6,2 @@ 'use strict'; | ||
module.exports = { | ||
/** | ||
@@ -18,3 +17,3 @@ * Returns a match of specific string to a first given pattern. If there are no matches to given string - | ||
*/ | ||
matchPattern(str, patterns, index = 0) { | ||
matchPattern (str, patterns, index = 0) { | ||
if (!str) { | ||
@@ -24,4 +23,20 @@ return null; | ||
const pattern = patterns.find(p => str.match(p)); | ||
if (!this.isArrayOfType(patterns, RegExp)) { | ||
throw Error('Patterns array must contain only RegExp patterns'); | ||
} | ||
const pattern = _.find(patterns, p => str.match(p)); | ||
return pattern ? str.match(pattern)[index] : null; | ||
}, | ||
/** | ||
* Verifies whether the given array consists of elements only of specific type. | ||
* | ||
* @param {Array} array Array to check. | ||
* @param {Function} type Constructor of specific type to verify. | ||
* @returns {boolean} Is a given array consists of only of given types. | ||
*/ | ||
isArrayOfType (array, type) { | ||
return _.isArray(array) && array.every(x => x instanceof type); | ||
} | ||
@@ -48,6 +63,6 @@ }; | ||
} | ||
return this.slice(start, lastIndex); | ||
}; | ||
/** | ||
@@ -54,0 +69,0 @@ * Creates a flattened array of values by running each element in given array |
{ | ||
"name": "newman-reporter-reportportal", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Reportportal reporter for newman", | ||
@@ -27,4 +27,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"eslint": "^5.12.1" | ||
"eslint": "^5.12.1", | ||
"eslint-plugin-jsdoc": "^4.1.0", | ||
"eslint-plugin-lodash": "^5.1.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77080
376
3