UNPKG

2.19 kBPlain TextView Raw
1import { Logger } from '@stryker-mutator/api/logging';
2import { FailedTestResult, TestResult, SuccessTestResult, TestStatus } from '@stryker-mutator/api/test-runner';
3import { I } from '@stryker-mutator/util';
4
5import { Timer } from './timer.js';
6
7export class StrykerMochaReporter {
8 /*
9 * The stryker logger instance injected into this plugin
10 * Needs to be set from 'the outside' because mocha doesn't really have a nice way of providing
11 * data to reporters...
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}