This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
JestTestRunner.ts
49 lines (38 loc) · 1.72 KB
/
JestTestRunner.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
import { RunnerOptions, RunResult, TestRunner, RunStatus, TestResult, TestStatus } from 'stryker-api/test_runner';
import { EventEmitter } from 'events';
import JestTestAdapterFactory from './jestTestAdapters/JestTestAdapterFactory';
export default class JestTestRunner extends EventEmitter implements TestRunner {
private jestConfig: any;
private projectRoot: string;
public constructor(options: RunnerOptions) {
super();
this.projectRoot = process.cwd();
this.jestConfig = options.strykerOptions.jest.config;
this.jestConfig.rootDir = this.projectRoot;
}
public async run(): Promise<RunResult> {
const jestTestRunner = JestTestAdapterFactory.getJestTestAdapter();
const { results } = await jestTestRunner.run(this.jestConfig, process.cwd());
// Map the failureMessages from each testSuite to a single array then filter out empty errors
const errorMessages = results.testResults.map((testSuite: any) => testSuite.failureMessage).filter((errorMessage: string) => errorMessage);
return {
tests: this.processTestResults(results.testResults),
status: (results.numRuntimeErrorTestSuites > 0) ? RunStatus.Error : RunStatus.Complete,
errorMessages
};
}
private processTestResults(suiteResults: Array<any>): Array<TestResult> {
const testResults: Array<TestResult> = [];
for (let suiteResult of suiteResults) {
for (let testResult of suiteResult.testResults) {
testResults.push({
name: testResult.fullName,
status: (testResult.status === 'passed') ? TestStatus.Success : TestStatus.Failed,
timeSpentMs: testResult.duration,
failureMessages: testResult.failureMessages
});
}
}
return testResults;
}
}