Skip to content

Instantly share code, notes, and snippets.

@Lgofk
Created May 28, 2021 19:55
Show Gist options
  • Save Lgofk/6bf1ea1e2dfad8d61db75c3bf5bff231 to your computer and use it in GitHub Desktop.
Save Lgofk/6bf1ea1e2dfad8d61db75c3bf5bff231 to your computer and use it in GitHub Desktop.
NestJS coverage configuration for combined unit & e2e test runs
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "..",
"testMatch": ["**/*.e2e-spec.ts"],
"testPathIgnorePatterns": ["/node_modules/", "/src/"],
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageReporters": ["json"],
"coverageDirectory": "coverage/e2e",
"collectCoverageFrom": ["src/**"],
"coveragePathIgnorePatterns": [
".module.ts$",
".spec.ts$",
],
"testEnvironment": "node"
}
/* SOURCE: https://github.com/facebook/jest/issues/2418#issuecomment-478932514 */
/* tslint:disable:no-console */
/*
ts-node ./merge-coverage.ts --report ./coverage0/coverage-final.json --report ./coverage1/coverage-final.json
*/
import * as fs from 'fs-extra';
import {createReporter} from 'istanbul-api';
import {createCoverageMap} from 'istanbul-lib-coverage';
import * as yargs from 'yargs';
main().catch((err) => {
console.error(err);
process.exit(1);
});
async function main() {
const argv = yargs
.options({
report: {
type: 'array', // array of string
desc: 'Path of json coverage report file',
demandOption: true,
},
reporters: {
type: 'array',
default: ['json', 'lcov', 'text'],
},
})
.argv;
const reportFiles = argv.report as string[];
const reporters = argv.reporters as string[];
const map = createCoverageMap({});
reportFiles.forEach((file) => {
const r = fs.readJsonSync(file);
map.merge(r);
});
const reporter = createReporter();
await reporter.addAll(reporters);
reporter.write(map);
console.log('Created a merged coverage report in ./coverage');
}

Setup

  1. Edit package.json Jest configuration as shown below. The big points are to ensure you have a json reporter and to ensure your coverage is stored in a unique directory for unit tests (in this config its coverage/unit. Also, add the development dependencies shown as they are required for the merge script.
  2. Edit your e2e configuration (test/jest-e2e.json) as shown below. The required "trick" was to ensure that rootDir's subtree includes paths for the e2e tests as well as an source you want to generate coverage for. Using the parent directory includes both test and src so this works. I tried many other methods, like setting collectCoverageFrom to ../src, none of them worked. It appears you must use the parent directory and exclude everything you don't want. Note that it also uses a unique directory for its generated coverage data (coverage/e2e).
  3. Add merge-coverage.ts shown below to your project and run it after you've ran both test sets generating coverage. You run the script passing in the reports you want to merge and it will generate a merged json, html and print the merged summary. Note that it's a TypeScript file so you need use ts-node or another method to execute it
{
"devDependencies": [
"fs-extra": "latest",
"istanbul-api": "latest"
],
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageReporters": [
"json"
],
"coverageDirectory": "../coverage/unit",
"coveragePathIgnorePatterns": [
".module.ts$",
".spec.ts$",
],
"testEnvironment": "node"
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment