UNPKG

2.14 kBPlain TextView Raw
1import path from 'path';
2
3import { AggregatedResult } from '@jest/test-result';
4
5/**
6 * Verifies that coverage is reported for all files and, if not, will return an error message
7 * @param results The jest test run result
8 * @param fileNamesWithMutantCoverage the file names for which coverage was reported
9 */
10export 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
21const MAX_FILES_IN_HINT = 3;
22function 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
33You probably configured a test environment in jest that is not reporting code coverage to Stryker.
34See also https://jestjs.io/docs/en/configuration.html#testenvironment-string
35
36Are 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
41If you're using your own custom test environment, please enrich the environment with this mixin:
42
43const { mixinJestEnvironment} = require('@stryker-mutator/jest-runner');
44module.exports = mixinJestEnvironment(MyCustomTestEnvironment);
45
46For more info, see https://stryker-mutator.io/docs/stryker-js/jest-runner#coverage-analysis`;
47}
48
\No newline at end of file