-
Notifications
You must be signed in to change notification settings - Fork 3
/
reporter.ts
68 lines (56 loc) · 2.07 KB
/
reporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import chalk from 'chalk';
import { reporters } from 'mocha';
import { TestRail } from './testrail';
import { TestRailOptions } from './testrail.interface';
import { titleToCaseId } from './utils';
export default class CypressTestrailReporter extends reporters.Base {
private readonly testRail: TestRail;
constructor(runner: Mocha.Runner, options: Mocha.MochaOptions) {
super(runner, options);
const reporterOptions = options.reporterOptions as TestRailOptions;
CypressTestrailReporter.validate(reporterOptions);
if (!reporterOptions.planId && !reporterOptions.runId) {
throw new Error('CypressTestRailReporter requires either `planId` or `runId` to be configured.');
}
if (!reporterOptions.projectId) {
throw new Error('CypressTestRailReporter required `projectId` to be configured.');
}
this.testRail = new TestRail(reporterOptions);
this.report();
}
private report() {
this.runner.once('start', this.testRail.constructSuites.bind(this.testRail));
this.runner.on('fail', this.handleTest('fail'));
this.runner.on('pass', this.handleTest('pass'));
this.runner.on('end', this.handleEnd);
}
private handleTest = (status: 'fail' | 'pass') => (test: Mocha.Test) => {
const caseId = titleToCaseId(test.title);
if (caseId) {
status === 'fail' ? this.testRail.addFailedTest(caseId, test) : this.testRail.addPassedTest(caseId, test);
}
};
private handleEnd = () => {
if (!this.testRail.results.length) {
console.log('\n', chalk.magenta.underline.bold('(TestRail Reporter)'));
console.warn(
'\n',
'No test cases were matched. Ensure that your tests are declared correctly and matches CXXXX',
'\n',
);
return;
}
this.testRail.publish();
};
private static validate(options: TestRailOptions) {
if (options == null) {
throw new Error('No reporterOptions');
}
for (const key in options) {
if ((options as any)[key]) {
continue;
}
throw new Error(`Unknown ${key} value, please update your reporterOptions.`);
}
}
}