- Edit
package.json
Jest configuration as shown below. The big points are to ensure you have ajson
reporter and to ensure your coverage is stored in a unique directory for unit tests (in this config itscoverage/unit
. Also, add the development dependencies shown as they are required for the merge script. - Edit your e2e configuration (
test/jest-e2e.json
) as shown below. The required "trick" was to ensure thatrootDir
's subtree includes paths for the e2e tests as well as an source you want to generate coverage for. Using the parent directory includes bothtest
andsrc
so this works. I tried many other methods, like settingcollectCoverageFrom
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
). - 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 mergedjson
,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
Created
August 30, 2022 07:23
-
-
Save kermitsxb/1a1a5f58a1d5d98b2f129b882747fe18 to your computer and use it in GitHub Desktop.
NestJS coverage configuration for combined unit & e2e test runs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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