1 | import path from 'path';
|
2 |
|
3 | import { AggregatedResult } from '@jest/test-result';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export function verifyAllTestFilesHaveCoverage(results: AggregatedResult, fileNamesWithMutantCoverage: Set<string>): string | undefined {
|
11 | const allTestFiles = new Set<string>(results.testResults.map(({ testFilePath }) => path.resolve(testFilePath)));
|
12 | const fullFileNamesWithCoverage = [...fileNamesWithMutantCoverage].map((fileName) => path.resolve(fileName));
|
13 | const missing = [...allTestFiles].filter((testFile) => !fullFileNamesWithCoverage.includes(testFile));
|
14 | if (missing.length > 0) {
|
15 | return formatError(missing);
|
16 | } else {
|
17 | return;
|
18 | }
|
19 | }
|
20 |
|
21 | const MAX_FILES_IN_HINT = 3;
|
22 | function formatError(coverageMissingFromFiles: string[]) {
|
23 | let fileHints = coverageMissingFromFiles
|
24 | .slice(0, MAX_FILES_IN_HINT)
|
25 | .map((fullName) => ` * ${path.relative(process.cwd(), fullName)}`)
|
26 | .join('\n');
|
27 | if (coverageMissingFromFiles.length > MAX_FILES_IN_HINT) {
|
28 | fileHints += `\n (and ${coverageMissingFromFiles.length - MAX_FILES_IN_HINT} more)`;
|
29 | }
|
30 | return `\nMissing coverage results for:
|
31 | ${fileHints}
|
32 |
|
33 | You probably configured a test environment in jest that is not reporting code coverage to Stryker.
|
34 | See also https://jestjs.io/docs/en/configuration.html#testenvironment-string
|
35 |
|
36 | Are you using node, jsdom or jsdom-sixteen as a test environment? Please change that:
|
37 | * node -> @stryker-mutator/jest-runner/jest-env/node
|
38 | * jsdom -> @stryker-mutator/jest-runner/jest-env/jsdom
|
39 | * jest-environment-jsdom-sixteen -> @stryker-mutator/jest-runner/jest-env/jsdom-sixteen
|
40 |
|
41 | If you're using your own custom test environment, please enrich the environment with this mixin:
|
42 |
|
43 | const { mixinJestEnvironment} = require('@stryker-mutator/jest-runner');
|
44 | module.exports = mixinJestEnvironment(MyCustomTestEnvironment);
|
45 |
|
46 | For more info, see https://stryker-mutator.io/docs/stryker-js/jest-runner#coverage-analysis`;
|
47 | }
|
48 |
|
\ | No newline at end of file |