1 | import { Logger } from '@stryker-mutator/api/logging';
|
2 | import { FailedTestResult, TestResult, SuccessTestResult, TestStatus } from '@stryker-mutator/api/test-runner';
|
3 | import { I } from '@stryker-mutator/util';
|
4 |
|
5 | import { Timer } from './timer.js';
|
6 |
|
7 | export class StrykerMochaReporter {
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
13 | public static log: Logger | undefined;
|
14 | private readonly timer = new Timer();
|
15 | private passedCount = 0;
|
16 | public tests: TestResult[] = [];
|
17 |
|
18 | public static currentInstance: I<StrykerMochaReporter> | undefined;
|
19 |
|
20 | constructor(private readonly runner: NodeJS.EventEmitter) {
|
21 | this.registerEvents();
|
22 | StrykerMochaReporter.currentInstance = this;
|
23 | }
|
24 |
|
25 | private registerEvents() {
|
26 | this.runner.on('start', () => {
|
27 | this.passedCount = 0;
|
28 | this.timer.reset();
|
29 | this.tests = [];
|
30 | StrykerMochaReporter.log?.debug('Starting Mocha test run');
|
31 | });
|
32 |
|
33 | this.runner.on('pass', (test: Mocha.Test) => {
|
34 | const title: string = test.fullTitle();
|
35 | const result: SuccessTestResult = {
|
36 | id: title,
|
37 | name: title,
|
38 | status: TestStatus.Success,
|
39 | timeSpentMs: this.timer.elapsedMs(),
|
40 | };
|
41 | this.tests.push(result);
|
42 | this.passedCount++;
|
43 | this.timer.reset();
|
44 | });
|
45 |
|
46 | this.runner.on('fail', (test: Mocha.Hook | Mocha.Test, err: Error) => {
|
47 | const title = test.ctx?.currentTest?.fullTitle() ?? test.fullTitle();
|
48 | const result: FailedTestResult = {
|
49 | id: title,
|
50 | failureMessage: (err.message || err.stack) ?? '<empty failure message>',
|
51 | name: title,
|
52 | status: TestStatus.Failed,
|
53 | timeSpentMs: this.timer.elapsedMs(),
|
54 | };
|
55 | this.tests.push(result);
|
56 | if (StrykerMochaReporter.log?.isTraceEnabled()) {
|
57 | StrykerMochaReporter.log?.trace(`Test failed: ${test.fullTitle()}. Error: ${err.message}`);
|
58 | }
|
59 | });
|
60 |
|
61 | this.runner.on('end', () => {
|
62 | StrykerMochaReporter.log?.debug('Mocha test run completed: %s/%s passed', this.passedCount, this.tests.length);
|
63 | });
|
64 | }
|
65 | }
|