testrail-jest-reporter
Advanced tools
Comparing version 1.0.5 to 1.0.6
20
index.js
@@ -71,4 +71,4 @@ 'use strict'; | ||
_testResults.testResults.forEach((result) => { | ||
const testcase = this._utils.formatCase(result); | ||
if (testcase) this._accumulateResults(testcase); | ||
const testcases = this._utils.formatCase(result); | ||
if (testcases) this._accumulateResults(testcases); | ||
}); | ||
@@ -98,9 +98,11 @@ } | ||
} | ||
_accumulateResults(testcase) { | ||
let index = -1; | ||
const test = this.tests.find(test => test.case_id === testcase.case_id); | ||
const run_id = test && test.run_id; | ||
if (run_id && !!this.results.length) index = this.results.findIndex(run => run.id === run_id); | ||
if (~index) this.results[index].results.push(testcase); | ||
else if (run_id) this.results.push({id: run_id, "results": [testcase]}); | ||
_accumulateResults(testcases_list) { | ||
for (let i=0, len = testcases_list.length; i<len; i++) { | ||
let index = -1; | ||
const test = this.tests.find(test => test.case_id === testcases_list[i].case_id); | ||
const run_id = test && test.run_id; | ||
if (run_id && !!this.results.length) index = this.results.findIndex(run => run.id === run_id); | ||
if (~index) this.results[index].results.push(testcases_list[i]); | ||
else if (run_id) this.results.push({id: run_id, "results": [testcases_list[i]]}); | ||
} | ||
} | ||
@@ -107,0 +109,0 @@ } |
{ | ||
"name": "testrail-jest-reporter", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Custom Jest reporter for Testrail synchronization", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -67,10 +67,13 @@ [![TestRail v6.7](https://img.shields.io/badge/TestRail%20API-v2-green.svg)](http://docs.gurock.com/testrail-api2/start) [![NPM](https://img.shields.io/npm/l/testrail-jest-reporter)](https://github.com/AntonChaukin/testrail-jest-reporter/blob/main/LICENSE) [![NPM](https://img.shields.io/node/v/testrail-jest-reporter)](https://github.com/AntonChaukin/testrail-jest-reporter/blob/main/package.json) | ||
The **testrail.conf.js** file needs to be created in your project's root folder. | ||
- It must contain the URL of your TestRail server, username (email address) and password (or API key). | ||
- This file needs to have all 3 parameters correctly filled. | ||
- It must contain your TestRail username (email address) and password (or API key). | ||
- This file needs to have all 2 parameters correctly filled. | ||
- It may contain the URL of your TestRail server as a `baseUrl` parameter, or <br>it can be specified in | ||
[Jest configuration](https://github.com/AntonChaukin/testrail-jest-reporter#jest-configurations) | ||
- You can specify custom regex expresion _(default: `/[C][?\d]{3,6}/gm`)_ | ||
### Use TestRail Milestone | ||
The first version of Reporter requires you to use a milestone. | ||
The first version of the Reporter requires you to use a milestone. | ||
- Use TestRail Milestone to versioning your tests. | ||
- **testrail.conf.js** file needs to have Milestone name filled. Or <br>it can be specified in | ||
[Jest configuration](https://github.com/AntonChaukin/testrail-jest-reporter#jest-configurations) | ||
- You can specify custom regex expresion _(default: `/[?\d]{3,6}/gm`)_ | ||
#### Example | ||
@@ -83,6 +86,7 @@ ```js | ||
'milestone': '<milestone_name>', | ||
'regex': /[?\d]{3,6}/gm | ||
'regex': /[C][?\d]{3,6}/gm | ||
} | ||
``` | ||
**Note:** If you use a public repository, please, secure your sensitive data. | ||
##### **Important:** If you use a public repository, please, secure your sensitive data. | ||
### Enable TestRail API | ||
@@ -95,3 +99,3 @@ In order to use [TestRail API](http://docs.gurock.com/testrail-api2/start), it needs to be enabled by an administrator | ||
### Add TestRail tests Runs | ||
In first version the Reporter needs you to add tests Runs with all tests you want to automate. | ||
The first version of the Reporter requires you to add tests Runs with all tests you want to automate. | ||
The Reporter parse all TestRail tests Plans | ||
@@ -107,2 +111,3 @@ <br>and test Runs of the Milestone to collect testcases. | ||
each test result you want to push to TestRail. | ||
You can specify several cases in one **_it()_** description. | ||
#### Usage | ||
@@ -116,3 +121,3 @@ ```javascript | ||
it("Test fail C124", async () => { | ||
it("Test fail C124 C125", async () => { | ||
expect(1).toBe(0); | ||
@@ -137,2 +142,3 @@ }); | ||
- ~~Also need to write more tests.~~ >> **Done in 1.0.4** | ||
- ~~Added ability to specify several case_ids in one test description~~ >> **Done in 1.0.6** | ||
@@ -139,0 +145,0 @@ <br>**Version 2:** |
'use strict'; | ||
const chalk = require('chalk'), tr_api = require('./interface'), Utils = require('./utils'); | ||
const error = chalk.bold.red, warning = chalk.keyword('orange'); | ||
const chalk = require('chalk'), tr_api = require('./interface'), Utils = require('./utils'), | ||
ReporterError = require('./error'); | ||
const error = chalk.bold.red; | ||
const util = new Utils(); | ||
@@ -22,9 +23,21 @@ | ||
if (!util.isArray(testsResults)) { | ||
console.log(error(`! Testrail Jest Reporter Error !`)); | ||
console.log(warning(`Something was wrong with tests results! \n\nContexts: ${JSON.stringify(testsResults)}`)); | ||
return Promise.resolve(false); | ||
throw new ReporterError(`Something was wrong with tests results! | ||
\n\nContexts: ${JSON.stringify(testsResults)}`); | ||
} | ||
return Promise.all( | ||
testsResults.map(run => tr_api.add_results_for_cases(run.id, {"results": run.results})) | ||
).then(response => { | ||
testsResults.map(run => { | ||
if (!util.isArray(run.results)) { | ||
throw new ReporterError(`The Run results is not an array! | ||
\n\n Context: ${JSON.stringify(run.results)}`); | ||
} | ||
for(let i=0, len=run.results.length; i<len; i++) { | ||
if (!util.isPlainObject(run.results[i])) { | ||
throw new ReporterError(`Something was wrong with the Run results! | ||
\n\n Context: ${JSON.stringify(run.results)}`); | ||
} | ||
} | ||
return tr_api.add_results_for_cases(run.id, {"results": run.results}); | ||
}) | ||
) | ||
.then(response => { | ||
let count = 0; | ||
@@ -40,8 +53,7 @@ response.map(run => { | ||
case 500: | ||
throw Error(run.error); | ||
throw new ReporterError(run.error); | ||
default: | ||
console.log(error(`TestRail API add_results_for_cases resolved ${JSON.stringify(run)}`)); | ||
break; | ||
throw new ReporterError(`TestRail API add_results_for_cases resolved ${JSON.stringify(run)}`); | ||
} | ||
} else console.log(error(`TestRail API add_results_for_cases resolved ${JSON.stringify(run)}`)); | ||
} else throw new ReporterError(`TestRail API add_results_for_cases resolved ${JSON.stringify(run)}`); | ||
}); | ||
@@ -65,3 +77,3 @@ return count; | ||
_milestone = res.filter((milestone) => milestone.name === this._milestone_name); | ||
} else console.log(error(`TestRail API get_milestones resolved ${JSON.stringify(res)}`)); | ||
} else throw new ReporterError(`TestRail API get_milestones resolved ${JSON.stringify(res)}`); | ||
@@ -78,7 +90,7 @@ if (_milestone && !!_milestone.length) { | ||
} | ||
else if (this._milestone_id) console.log(error(`TestRail API get_plans resolved ${JSON.stringify(res)}`)); | ||
else if (this._milestone_id) throw new ReporterError(`TestRail API get_plans resolved ${JSON.stringify(res)}`); | ||
return false; | ||
}) | ||
.catch((err) => { | ||
console.log(error(err)); | ||
console.log(error(err.stack)); | ||
return false; | ||
@@ -102,3 +114,3 @@ }) | ||
.catch((err) => { | ||
console.log(error(err)); | ||
console.log(error(err.stack)); | ||
return false; | ||
@@ -132,10 +144,9 @@ }) | ||
} | ||
console.log(error(`! Testrail Jest Reporter Error !`)); | ||
console.log(warning(`There is no one Testrail testcase was finding in Project ${this._project_id} by milestone ${this._milestone_name}`)); | ||
return false; | ||
throw new ReporterError(`There is no one Testrail testcase was finding in Project id=${this._project_id} | ||
by milestone "${this._milestone_name}"`); | ||
}) | ||
.catch((err) => { | ||
console.log(error(err)); | ||
console.log(error(err.stack)); | ||
return false; | ||
}); | ||
} |
@@ -5,3 +5,3 @@ 'use strict'; | ||
constructor(_options) { | ||
this._regex = _options && _options.regex || /[?\d]{3,6}/gm; | ||
this._regex = _options && _options.regex || /[C][?\d]{3,6}/gm; | ||
this._statuses = _options && _options.statuses || {}; | ||
@@ -16,4 +16,5 @@ this._status = { | ||
formatCase(testResult) { | ||
const case_id = this._formatTitle(testResult.title); | ||
if (case_id) { | ||
let cases = []; | ||
const cases_ids = this._formatTitle(testResult.title); | ||
if (cases_ids) { | ||
const message = !!testResult.failureMessages.length ? testResult.failureMessages : `**${testResult.status}**`; | ||
@@ -25,10 +26,13 @@ const elapsed = this._formatTime(testResult.duration); | ||
+ '\n' + message; | ||
return { | ||
"case_id": parseInt(case_id), | ||
"status_id": status_id, | ||
"comment": comment, | ||
"elapsed": elapsed || "", | ||
"defects": "", | ||
"version": "", | ||
for (let i=0, len = cases_ids.length; i<len; i++) { | ||
cases.push({ | ||
"case_id": parseInt(cases_ids[i]), | ||
"status_id": status_id, | ||
"comment": comment, | ||
"elapsed": elapsed || "", | ||
"defects": "", | ||
"version": "", | ||
}) | ||
} | ||
return cases; | ||
} | ||
@@ -61,4 +65,9 @@ return false; | ||
const regex = this._regex; | ||
const _t = title.match(regex); | ||
return _t && _t[0]; | ||
const case_ids = []; | ||
let _t = title.match(regex); | ||
_t = _t || []; | ||
for (let i=0, len=_t.length; i<len; i++) { | ||
case_ids.push(_t[i].match(/[?\d]{3,6}/gm)) | ||
} | ||
return case_ids.length && case_ids; | ||
} | ||
@@ -65,0 +74,0 @@ |
@@ -1,2 +0,3 @@ | ||
const faker = require('faker'); | ||
const faker = require('faker'), Utils = require('../src/utils'); | ||
const utils = new Utils(); | ||
const increaser = count(5); | ||
@@ -18,5 +19,19 @@ | ||
function passed(cid) { | ||
const _duration = duration(); | ||
const _case_title = case_title(_duration, cid); | ||
/** | ||
* | ||
* @param {object} options | ||
* @param {boolean} options.cid | ||
* @param {number} options.cid_count | ||
* @return {{duration: *, failureMessages: [], fullName: string, location: null, numPassingAsserts: number, title: (string|*), ancestorTitles: [string], status: string}} | ||
*/ | ||
function passed(options) { | ||
options = options || {cid: true, cid_count: 1}; | ||
const count = options.cid_count || 1; | ||
const _duration = []; | ||
let i = 0; | ||
do { | ||
i++; | ||
_duration.push(duration()); | ||
} while (i<count) | ||
const _case_title = case_title(_duration, options.cid); | ||
return { | ||
@@ -26,3 +41,3 @@ "ancestorTitles": [ | ||
], | ||
"duration": _duration, | ||
"duration": _duration[0], | ||
"failureMessages": [], | ||
@@ -76,3 +91,7 @@ "fullName": "Reporter tests "+_case_title, | ||
function name() {return faker.random.words()} | ||
function name() { | ||
let _name = faker.random.words(); | ||
if (_name.length < 3) _name += name(); | ||
return _name.slice(0,16); | ||
} | ||
@@ -83,5 +102,10 @@ function case_title(duration, cid = true) { | ||
if (cid) { | ||
const _index = index(); | ||
string.push(_title.slice(0,_index)); | ||
string.push('C'+ duration); | ||
duration = utils.isArray(duration) ? duration : [duration]; | ||
let _index = 0; | ||
for (let i=0, len=duration.length; i<len; i++) { | ||
const start = _index; | ||
_index += index(); | ||
string.push(_title.slice(start,_index)); | ||
string.push('C'+ duration[i]); | ||
} | ||
string.push(_title.slice(_index)); | ||
@@ -88,0 +112,0 @@ return string.join(' '); |
@@ -1,2 +0,2 @@ | ||
const Utils = require('../src/utils'); | ||
const Utils = require('../src/utils'), ReporterError = require('../src/error'); | ||
const chalk = require('chalk'); | ||
@@ -15,6 +15,15 @@ const error = chalk.bold.red; | ||
const _duration = duration(); | ||
let case_id = utils._formatTitle(case_title(_duration,true)); | ||
let [case_id] = utils._formatTitle(case_title(_duration,true)); | ||
expect(parseInt(case_id)).toEqual(_duration); | ||
}); | ||
it('Should parse several case id from test title', () => { | ||
const utils = new Utils(); | ||
const duration_1 = duration(); | ||
const duration_2 = duration(); | ||
let [case_id_1, case_id_2] = utils._formatTitle(case_title([duration_1, duration_2],true)); | ||
expect(parseInt(case_id_1)).toEqual(duration_1); | ||
expect(parseInt(case_id_2)).toEqual(duration_2); | ||
}); | ||
it('Should parse elapsed time from test duration', () => { | ||
@@ -28,4 +37,4 @@ const utils = new Utils(); | ||
const utils = new Utils(); | ||
const testResult = passed(true); | ||
let _case = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
let [_case] = utils.formatCase(testResult); | ||
expect(_case.status_id).toEqual(1); | ||
@@ -36,5 +45,15 @@ expect(_case.comment.includes('passed')).toBeTruthy(); | ||
it('Should parse Jest passed result with several cid', () => { | ||
const utils = new Utils(); | ||
const testResult = passed({cid: true, cid_count: 2}); | ||
let [case_1, case_2] = utils.formatCase(testResult); | ||
expect(case_1.status_id).toEqual(1); | ||
expect(case_2.status_id).toEqual(1); | ||
expect(case_1.comment.includes('passed')).toBeTruthy(); | ||
expect(case_1.case_id).toEqual(testResult.duration); | ||
}); | ||
it('Return false if Jest result without cid', () => { | ||
const utils = new Utils(); | ||
let _case = utils.formatCase(passed(false)); | ||
let _case = utils.formatCase(passed({cid: false})); | ||
expect(_case).toBeFalsy(); | ||
@@ -46,3 +65,3 @@ }); | ||
const testResult = failed(true); | ||
let _case = utils.formatCase(testResult); | ||
let [_case] = utils.formatCase(testResult); | ||
expect(_case.status_id).toEqual(5); | ||
@@ -55,3 +74,3 @@ expect(_case.comment.includes('Error')).toBeTruthy(); | ||
const utils = new Utils(); | ||
let _case = utils.formatCase(pending(true)); | ||
let [_case] = utils.formatCase(pending(true)); | ||
expect(_case.status_id).toEqual(4); // default pending status_id | ||
@@ -128,3 +147,3 @@ expect(_case.comment.includes('pending')).toBeTruthy(); | ||
reporter.tests = [{ok: true}]; | ||
const testResult = passed(false); | ||
const testResult = passed({cid: false}); | ||
await reporter.onTestResult(null, {testResults: [testResult]},null); | ||
@@ -140,3 +159,3 @@ expect(spy).toHaveBeenCalledTimes(1); | ||
reporter.tests = [{ok: true}]; | ||
const testResult = passed(false); | ||
const testResult = passed({cid: false}); | ||
await reporter.onTestResult(null, {testResults: [testResult]},null); | ||
@@ -152,7 +171,7 @@ expect(spy).toHaveBeenCalledTimes(0); | ||
reporter.tests = [{ok: true}]; | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const testcase_list = utils.formatCase(testResult); | ||
await reporter.onTestResult(null, {testResults: [testResult]},null); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith(testcase); | ||
expect(spy).toHaveBeenCalledWith(testcase_list); | ||
spy.mockRestore(); | ||
@@ -164,4 +183,4 @@ }); | ||
let utils = new Utils(); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
reporter.tests = [{case_id: testResult.duration, run_id: 1}]; | ||
@@ -177,6 +196,6 @@ await reporter.onTestResult(null, {testResults: [testResult]},null); | ||
let utils = new Utils(); | ||
const testResult_1 = passed(true); | ||
const testResult_1 = passed(); | ||
const testResult_2 = failed(true); | ||
const testcase_1 = utils.formatCase(testResult_1); | ||
const testcase_2 = utils.formatCase(testResult_2); | ||
const [testcase_1] = utils.formatCase(testResult_1); | ||
const [testcase_2] = utils.formatCase(testResult_2); | ||
reporter.tests = [{case_id: testResult_1.duration, run_id: 1}, {case_id: testResult_2.duration, run_id: 1}]; | ||
@@ -192,6 +211,6 @@ await reporter.onTestResult(null, {testResults: [testResult_1, testResult_2]},null); | ||
let utils = new Utils(); | ||
const testResult_1 = passed(true); | ||
const testResult_1 = passed(); | ||
const testResult_2 = failed(true); | ||
const testcase_1 = utils.formatCase(testResult_1); | ||
const testcase_2 = utils.formatCase(testResult_2); | ||
const [testcase_1] = utils.formatCase(testResult_1); | ||
const [testcase_2] = utils.formatCase(testResult_2); | ||
reporter.tests = [{case_id: testResult_1.duration, run_id: 1}, {case_id: testResult_2.duration, run_id: 2}]; | ||
@@ -209,5 +228,5 @@ await reporter.onTestResult(null, {testResults: [testResult_1, testResult_2]},null); | ||
let utils = new Utils(); | ||
const testResult_1 = passed(true); | ||
const testResult_1 = passed(); | ||
const testResult_2 = failed(true); | ||
const testcase_1 = utils.formatCase(testResult_1); | ||
const [testcase_1] = utils.formatCase(testResult_1); | ||
reporter.tests = [{case_id: testResult_1.duration, run_id: 1}]; | ||
@@ -242,4 +261,4 @@ await reporter.onTestResult(null, {testResults: [testResult_1, testResult_2]},null); | ||
const utils = new Utils(); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
const results = [{id: 1, results: [testcase]}]; | ||
@@ -640,4 +659,4 @@ reporter.results = results; | ||
let utils = new Utils(); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
const resp = {statusCode: 200, body: [tr_result(testcase)]}; | ||
@@ -654,2 +673,50 @@ | ||
it('add_results with wrong results: array', async() => { | ||
const caller = require('../src/caller'); | ||
let utils = new Utils(); | ||
const testResult = passed(); | ||
const testcase = utils.formatCase(testResult); | ||
let resp = null; | ||
try { | ||
resp =await caller.add_results([{id: 1, results: [testcase]}]); | ||
} | ||
catch (err) { | ||
expect(err.name).toEqual('Testrail Jest Reporter Error'); | ||
} | ||
expect(resp).toBeFalsy(); | ||
}); | ||
it('add_results with wrong results: object', async() => { | ||
const caller = require('../src/caller'); | ||
let utils = new Utils(); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
let resp = null; | ||
try { | ||
resp =await caller.add_results([{id: 1, results: testcase}]); | ||
} | ||
catch (err) { | ||
expect(err.name).toEqual('Testrail Jest Reporter Error'); | ||
} | ||
expect(resp).toBeFalsy(); | ||
}); | ||
it('add_results with wrong results: not an object', async() => { | ||
const caller = require('../src/caller'); | ||
let utils = new Utils(); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
let resp = null; | ||
try { | ||
resp =await caller.add_results([{id: 1, results: [testcase, 'test']}]); | ||
} | ||
catch (err) { | ||
expect(err.name).toEqual('Testrail Jest Reporter Error'); | ||
} | ||
expect(resp).toBeFalsy(); | ||
}); | ||
it('add_results add_results_for_cases return error', async() => { | ||
@@ -660,4 +727,4 @@ const api = require('../src/interface'); | ||
let utils = new Utils(); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
const err = new Error('Request rejected'); | ||
@@ -673,3 +740,3 @@ const resp = {statusCode: 500, error: err}; | ||
expect(res).toBeFalsy(); | ||
expect(console_spy).toHaveBeenCalledWith(error("Error: "+err)); | ||
expect(console_spy).toHaveBeenCalledWith(error("Testrail Jest Reporter Error: "+err)); | ||
console_spy.mockRestore(); | ||
@@ -684,4 +751,4 @@ }); | ||
const err = new Error('Request rejected'); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
@@ -703,4 +770,4 @@ const add_results_for_cases_spy = jest.spyOn(api, 'add_results_for_cases') | ||
let utils = new Utils(); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
@@ -721,4 +788,5 @@ const add_results_for_cases_spy = jest.spyOn(api, 'add_results_for_cases') | ||
let utils = new Utils(); | ||
const testResult = passed(true); | ||
const testcase = utils.formatCase(testResult); | ||
const err = new ReporterError("TestRail API add_results_for_cases resolved undefined"); | ||
const testResult = passed(); | ||
const [testcase] = utils.formatCase(testResult); | ||
@@ -732,3 +800,3 @@ const add_results_for_cases_spy = jest.spyOn(api, 'add_results_for_cases') | ||
expect(res).toBeFalsy(); | ||
expect(console_spy).toHaveBeenCalledWith(error("TestRail API add_results_for_cases resolved undefined")); | ||
expect(console_spy).toHaveBeenCalledWith(error(err)); | ||
console_spy.mockRestore() | ||
@@ -735,0 +803,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
101992
14
1539
150