-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ng test: Collect total code coverage in multi project repository #11268
Comments
I've ran into a similar issue today. Our Angular workspace has one app and four libraries, each producing their own coverage results. I wanted a combined HTML report after all tests for all projects have run. I'm not sure if there's a better way, but so far I ended up with the following setup that seems to be working so far:
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../../coverage/libraries/<project>'),
reports: ['json'],
fixWebpackSourcePaths: true
},
"cover:library:<project>": "ng test <project> --code-coverage --watch=false",
"cover": "run-p cover:**",
"postcover": "istanbul report json && node combine-coverage.js",
const createReporter = require('istanbul-api').createReporter;
const istanbulCoverage = require('istanbul-lib-coverage');
const coverage = require('./coverage/coverage-final.json');
const map = istanbulCoverage.createCoverageMap();
Object.keys(coverage).forEach(filename => map.addFileCoverage(coverage[filename]));
const reporter = createReporter();
reporter.addAll(['html', 'lcovonly', 'text-summary']);
reporter.write(map); This way, when I execute The last step was necessary in order to produce a newer-style report with correct source paths and styles. At first, I was just running Hope this helps |
What is content in the coverage-final.json file? @dtychshenko |
There are multiple
In step 4, the first command The script in step 5 reads the last combined JSON report to produce Note that your mileage may vary with different library versions, but this setup currently works for us with the following:
I see that All you have to do in your combine-coverage.js script is instead of requiring the single combined JSON report file ( |
Thank you @dtychshenko for sharing your approach, it really helped here! Regarding the removal of the So, in this case, steps 4 and 5 could be replaced by a script of this kind:
|
@charliemc Could you post your package.json & karma.conf.js ? i get some known issue with istanbul-combine : Update : fixed with --coverage --source-map=true |
For those who still needs this, I manage to get the report to work with the following script. I used karma-coverage to return json format which then the script will merge and combine into other reporting format. Would be great if we can do this directly from the cli. var glob = require('glob');
var libCoverage = require('istanbul-lib-coverage');
var libReport = require('istanbul-lib-report');
var reports = require('istanbul-reports');
var map = libCoverage.createCoverageMap();
// Gets all the coverage files and merge them
var files = glob.sync('./tests/coverage/**/coverage-final.json');
files.forEach(f => map.merge(require(f)));
// Creates a context for report generation
var context = libReport.createContext({
dir: './tests/coverage',
coverageMap: map
});
var reportTypes = ['html', 'lcovonly', 'text-summary', 'cobertura'];
reportTypes.forEach(t => {
var report = reports.create(t);
report.execute(context);
}); |
Bug Report or Feature Request (mark with an
x
)Area
Versions
Repro steps
Multi project workspace, run
ng test
to test all projects.The log given by the failure
Produced coverage report in
coverage/lcov.info
only contains results for the last run code coverage.HTML reports are stored per project in
coverage/<project>
.Desired functionality
Write indivudual lcovs per project, e.g,
coverage/<project>/lcov.info
, allowing users to get a total code coverage for the whole project.Alternative would be functionality similar to istanbul-combine. Write
coverage.json
for each project and write a combined coverage report and end of test runs.Mention any other details that might be useful
Should eht changes be made in the builder or in the schematic?
https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/library/files/__projectRoot__/karma.conf.js#L19
https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/application/files/root/karma.conf.js#L19
The text was updated successfully, but these errors were encountered: