testrail-jest-reporter
Advanced tools
Comparing version 1.0.1 to 1.0.2
25
index.js
'use strict'; | ||
const chalk = require('chalk'); | ||
const error = chalk.bold.red; | ||
const warning = chalk.keyword('orange'); | ||
const {formatCase} = require('./lib/utils'); | ||
@@ -26,4 +29,11 @@ const {add_results, get_tests} = require('./lib/caller'); | ||
onRunStart(_results, _options) { | ||
get_tests() | ||
.then(_tests => tests = _tests); | ||
if (this._options.project_id) { | ||
get_tests(this._options.project_id) | ||
.then(_tests => tests = _tests); | ||
} | ||
else { | ||
console.log(error(`! Testrail Jest Reporter Error !`)); | ||
console.log(warning(`You must define "project_id" in jets configurations! | ||
\n Example: "reporters": [ ["testrail-jest-reporter", { "project_id": "1" }] ]`)); | ||
} | ||
} | ||
@@ -49,7 +59,8 @@ | ||
onTestResult(_test, _testResults, _aggregatedResult) { | ||
//console.log(`onTestResult arguments: ${JSON.stringify(arguments, null, '\t')}`); | ||
_testResults.testResults.forEach((result) => { | ||
const testcase = formatCase(result); | ||
if (testcase) accumulateResults(testcase); | ||
}); | ||
if (tests) { | ||
_testResults.testResults.forEach((result) => { | ||
const testcase = formatCase(result); | ||
if (testcase) accumulateResults(testcase); | ||
}); | ||
} | ||
} | ||
@@ -56,0 +67,0 @@ |
@@ -5,3 +5,3 @@ const process = require('process'), path = require('path'), rp = require('request-promise'); | ||
const configPath = path.resolve(process.cwd(), DEFAULT_CONFIG_FILENAME); | ||
const {baseUrl, user, pass, project, milestone} = require(configPath); | ||
const {baseUrl, user, pass, milestone} = require(configPath); | ||
const base = baseUrl + '/index.php?/api/v2/'; | ||
@@ -42,4 +42,4 @@ const auth = {'user': user, 'pass': pass}; | ||
function get_milestone_id() { | ||
return get(url.get_milestones + project, {is_completed: 0}) | ||
function get_milestone_id(project_id) { | ||
return get(url.get_milestones + project_id, {is_completed: 0}) | ||
.then(res => res.reduce((id, _milestone) => { | ||
@@ -50,4 +50,4 @@ if (_milestone.name === milestone) return _milestone.id | ||
function get_plans_ids(milestone_id) { | ||
return get(url.get_plans + project, {is_completed: 0, milestone_id: milestone_id}) | ||
function get_plans_ids(project_id, milestone_id) { | ||
return get(url.get_plans + project_id, {is_completed: 0, milestone_id: milestone_id}) | ||
.then(res => res.map(plan => plan.id)); | ||
@@ -61,4 +61,4 @@ } | ||
function get_runs_ids(milestone_id) { | ||
return get(url.get_runs + project, {is_completed: 0, milestone_id: milestone_id}) | ||
function get_runs_ids(project_id, milestone_id) { | ||
return get(url.get_runs + project_id, {is_completed: 0, milestone_id: milestone_id}) | ||
.then(res => res.map(run => run.id)); | ||
@@ -72,8 +72,8 @@ } | ||
function get_tests() { | ||
return get_milestone_id() | ||
function get_tests(project_id) { | ||
return get_milestone_id(project_id) | ||
.then(_milestone_id => { | ||
if (_milestone_id) { | ||
milestone_id = _milestone_id; | ||
return get_plans_ids(milestone_id); | ||
return get_plans_ids(project_id, milestone_id); | ||
} | ||
@@ -91,3 +91,3 @@ return false; | ||
if (_run_ids) _run_ids.map(ids => runs_ids = runs_ids.concat(ids)); | ||
if (milestone_id) return get_runs_ids(milestone_id); | ||
if (milestone_id) return get_runs_ids(project_id, milestone_id); | ||
return false; | ||
@@ -110,5 +110,6 @@ }) | ||
console.log(error(`! Testrail Jest Reporter Error !`)); | ||
console.log(warning(`There is no one Testrail testcase was finding in Project ${project} by milestone ${milestone}`)); | ||
console.log(warning(`There is no one Testrail testcase was finding in Project ${project_id} by milestone ${milestone}`)); | ||
return false; | ||
}); | ||
}) | ||
.catch((err) => console.log(error(JSON.stringify(err)))); | ||
} | ||
@@ -151,3 +152,3 @@ | ||
return rp(options) | ||
.catch((err) => console.log(error(JSON.stringify(err)))); | ||
.catch((err) => throw new Error(err)); | ||
} |
@@ -5,2 +5,3 @@ const status = { | ||
}; | ||
const _regex = /[?\d]{3,6}/gm; | ||
@@ -10,8 +11,6 @@ module.exports = { | ||
formatCase: formatCase, | ||
getCaseId: getCaseId | ||
}; | ||
function formatCase(testResult) { | ||
const case_id = getCaseId(testResult.title); | ||
if (!!case_id) { | ||
if (testResult.title.match(_regex)) { | ||
const message = !!testResult.failureMessages.length ? testResult.failureMessages : `**${testResult.status}**`; | ||
@@ -24,3 +23,3 @@ const elapsed = formatTime(testResult.duration); | ||
return { | ||
"case_id": parseInt(case_id), | ||
"case_id": parseInt(testResult.title.match(_regex)[0]), | ||
"status_id": status_id, | ||
@@ -54,15 +53,2 @@ "comment": comment, | ||
return `${s}s`; | ||
} | ||
function getCaseId(str) { | ||
let number = ''; | ||
if (~str.indexOf('C')) { | ||
const index = str.indexOf('C')+1; | ||
if (isNaN(str[index])) return getCaseId(str.slice(index)) | ||
for (let i = index; i < str.length; i++) { | ||
if (isNaN(str[i])) break | ||
number = number + str[i]; | ||
} | ||
} | ||
return number; | ||
} |
{ | ||
"name": "testrail-jest-reporter", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Custom Jest reporter for Testrail synchronization", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=10.0.0" | ||
"node": ">=8.0.0" | ||
}, | ||
@@ -9,0 +9,0 @@ "scripts": { |
118
README.md
@@ -1,2 +0,116 @@ | ||
# testrail-jest-reporter | ||
Custom Jest reporter for Testrail synchronization | ||
[![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) | ||
# Jest to TestRail Reporter | ||
This package allows you to use [Jest](https://jestjs.io/) and report your test results to [TestRail](http://www.gurock.com/testrail/). | ||
<br>**Please use with combination with the default reporter** | ||
## Install | ||
```code | ||
npm i testrail-jest-reporter --save-dev | ||
``` | ||
## Jest configurations | ||
As per [Jest's documentation](https://facebook.github.io/jest/docs/en/configuration.html#reporters-array-modulename-modulename-options), | ||
the Reporter must be specified in the jest-config.js or package.json file as part of the `reporters` array. | ||
- This file should be created in your project's root folder. | ||
- Parameter is defined as 'project_id', which is the id of your project on TestRail. | ||
#### Usage | ||
```javascript | ||
// this is the part of the jest-config.js | ||
module.exports = { | ||
..., | ||
reporters: [ | ||
"default", | ||
["jest-2-testrail", { project_id: "1" }] | ||
], | ||
... | ||
}; | ||
``` | ||
```js | ||
// this is the "jest" part of the package.json | ||
{ | ||
"jest": { | ||
"reporters": [ | ||
"default", | ||
["jest-2-testrail", { "project_id": "1" }] | ||
] | ||
} | ||
} | ||
``` | ||
## TestRail configurations | ||
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. | ||
### Use TestRail Milestone | ||
In first version the Reporter needs you to use milestone. | ||
- Use TestRail Milestone to versioning your tests. | ||
- **testrail.conf.js** file needs to have Milestone name filled. | ||
#### Example | ||
```javascript | ||
module.exports = { | ||
'baseUrl': 'http://localhost', | ||
'user': 'user@example.com', | ||
'pass': 'some-password', | ||
'milestone': '<milestone_name>' | ||
} | ||
``` | ||
### Enable TestRail API | ||
In order to use [TestRail API](http://docs.gurock.com/testrail-api2/start), it needs to be enabled by an administrator | ||
<br>in your own TestRail Site Settings. | ||
Also, if you want to use API authentication instead of your password, | ||
<br>enable session authentication for API in the TestRail Site Settings, | ||
<br>and add an API key in your User settings _(This is recommended)_. | ||
### Add TestRail tests Runs | ||
In first version the Reporter needs you to add tests Runs with all tests you want to automate. | ||
The Reporter parse all TestRail tests Plans | ||
<br>and test Runs of the Milestone to collect testcases. | ||
The Reporter collects only unique testcases, | ||
<br>if you have several tests Runs with one testcase | ||
then The Reporter push the test result only to one of that Runs. | ||
## Example tests | ||
The Case ID from TestRail must be added to **_it()_** description | ||
each test result you want to push to TestRail. | ||
#### Usage | ||
```javascript | ||
describe("Tests suite", () => { | ||
// "C123" this is Case ID from Test Rail | ||
it("C123 test success", async () => { | ||
expect(1).toBe(1); | ||
}); | ||
it("Test fail C124", async () => { | ||
expect(1).toBe(0); | ||
}); | ||
xit("Another success test", async () => { | ||
expect(1).toBe(1); | ||
}); | ||
}); | ||
``` | ||
**Note:** The Case ID is a unique and permanent ID of every test case (e.g. C125), | ||
and shouldn't be confused with a Test Case ID, <br>which is assigned to a test case when a new run is created (e.g. T325). | ||
**Note**: The first and second **_it()_** test result will be reported, and the last - not. | ||
### Roadmap | ||
**This version:** | ||
- Add new tests Run if there are testcases that are not present in any of the existing TestRail tests Runs. | ||
- Add new test Runs if the Milestone not specified. | ||
- Also need to write more tests. | ||
<br>**Version 2:** | ||
- Add the Reporter CLI. | ||
<br>**Version 3:** | ||
- Add ability to parse code annotations. | ||
## License | ||
This project is licensed under the MIT License - see the [LICENSE](https://github.com/AntonChaukin/testrail-jest-reporter/blob/main/LICENSE) file for details. |
@@ -12,4 +12,3 @@ const baseUrl = process.env.URL; | ||
'pass': pass || 'some-password', | ||
//'project': '<project_id>', | ||
//'milestone': '<milestone_name>' | ||
} |
const faker = require('faker'); | ||
const index = faker.random.number(10); | ||
const duration = faker.random.number(); | ||
const index = faker.random.number(20); | ||
const duration = faker.random.number({min: 100, max: 99999}); | ||
const title = faker.lorem.sentence(); | ||
@@ -50,3 +50,2 @@ const _case = 'C'+duration; | ||
module.exports = { | ||
case_title: case_title, | ||
duration: duration, | ||
@@ -53,0 +52,0 @@ jest_results: results |
@@ -1,3 +0,3 @@ | ||
const {formatCase, getCaseId, formatTime} = require('../lib/utils'); | ||
const {duration, case_title, jest_results} = require('./sample'); | ||
const {formatCase, formatTime} = require('../lib/utils'); | ||
const {duration, jest_results} = require('./sample'); | ||
@@ -7,4 +7,4 @@ describe('Reporter tests', function (){ | ||
it('Should parse case id from test title', () => { | ||
let number = getCaseId(case_title); | ||
expect(parseInt(number)).toEqual(duration); | ||
let _case = formatCase(jest_results[0]); | ||
expect(_case.case_id).toEqual(duration); | ||
}); | ||
@@ -11,0 +11,0 @@ |
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
28967
116
12
367