I'm running tests in multiple projects of my nx angular monorepo and would like to get a single code coverage report of all projects with all code files covert from the tests. The test-runs seems to scope the analysed code to the current nx project and I do not get the coverage report from a base library (used from multiple projects). This might not be best practise, but I would like to analyse, which code is in use und which can be refactored / removed.
I have already tried some solution strategies, but none of them has resolved all my problems.
I have extended the jest.config.js of all my projects and add the coverage and test reporters (currently junit & cobertura for publish/display in Azure DevOps)
jest.config.js
module.exports = {
...
coverageReporters: ["cobertura", "html"],
...
reporters: [
"default",
["jest-junit", { outputDirectory: "coverage/libs/my-lib", outputName: "junit.xml" }]
],
collectCoverageFrom: [
"**/*.{ts,tsx}",
"!**/node_modules/**",
"!**/coverage/**",
"!**/vendor/**"
]
};
run all projects
I tried to run all tests in every app and library with the nx run-many command.
nx run-many --all --target=test --parallel -- --collectCoverage --coverage
I get a coverage folder for each with each test/coverage report and might be able to merge them to a single report (e.g. https://mcmap.net/q/437700/-is-there-anyway-to-merge-cobertura-coverage-xml-reports-together). But there were not all source files included and coverage runs was always scoped to one single project (usage of library code from multiple apps was not recorded).
run global jest config
The secound aproach was to run the global jest config directly throw jest.
node \"node_modules/jest/bin/jest.js\" -c \"jest.config.js\" --coverage --collectCoverage --coverageReporters=html --verbose=false
I think, this might be similar to the first approach, because jest also has the project configuration an run each project independently. I get one coverage and test report with all results in it. But there were also not all source files included and coverage runs was always scoped to one single project (usage of library code from multiple apps was not recorded).
module.exports = {
projects: getJestProjects(),
reporters: [
"default",
["jest-junit", { outputDirectory: "coverage", outputName: "junit.xml" }],
["./node_modules/jest-html-reporter", {
"pageTitle": "Test Report",
"outputPath": "coverage/test-report.html",
"includeConsoleLog": true,
"includeFailureMsg": true,
"includeSuiteFailure": true
}]
],
collectCoverageFrom: [
"**/*.{ts,tsx}",
"!**/node_modules/**",
"!**/coverage/**",
"!**/vendor/**"
]
};
Libraries
- angular 13
- jest 27