UNPKG

@stryker-mutator/mocha-runner

Version:

A plugin to use the mocha test runner in Stryker, the JavaScript mutation testing framework

59 lines 2.16 kB
import { TestStatus } from '@stryker-mutator/api/test-runner'; import { Timer } from './timer.js'; export class StrykerMochaReporter { runner; /* * The stryker logger instance injected into this plugin * Needs to be set from 'the outside' because mocha doesn't really have a nice way of providing * data to reporters... */ static log; timer = new Timer(); passedCount = 0; tests = []; static currentInstance; constructor(runner) { this.runner = runner; this.registerEvents(); StrykerMochaReporter.currentInstance = this; } registerEvents() { this.runner.on('start', () => { this.passedCount = 0; this.timer.reset(); this.tests = []; StrykerMochaReporter.log?.debug('Starting Mocha test run'); }); this.runner.on('pass', (test) => { const title = test.fullTitle(); const result = { id: title, name: title, status: TestStatus.Success, timeSpentMs: this.timer.elapsedMs(), fileName: test.file, }; this.tests.push(result); this.passedCount++; this.timer.reset(); }); this.runner.on('fail', (test, err) => { const title = test.ctx?.currentTest?.fullTitle() ?? test.fullTitle(); const result = { id: title, failureMessage: (err.message || err.stack) ?? '<empty failure message>', name: title, status: TestStatus.Failed, timeSpentMs: this.timer.elapsedMs(), }; this.tests.push(result); if (StrykerMochaReporter.log?.isTraceEnabled()) { StrykerMochaReporter.log?.trace(`Test failed: ${test.fullTitle()}. Error: ${err.message}`); } }); this.runner.on('end', () => { StrykerMochaReporter.log?.debug('Mocha test run completed: %s/%s passed', this.passedCount, this.tests.length); }); } } //# sourceMappingURL=stryker-mocha-reporter.js.map