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
feat(@angular-devkit/build-angular): code coverage for ng build, ng serve and ng e2e #13131
Conversation
Hi @divdavem! This PR has merge conflicts due to recent upstream merges. |
…d, ng serve and ng e2e This commit allows --code-coverage and --code-coverage-exclude options to be used with ng build, ng serve and ng e2e, so that it is easy to instrument the code for coverage. When used with these commands, the --code-coverage switch does not automatically create a file with the report but allows the developers to access the __coverage__ variable from Istanbul.js inside the browser to get coverage data. That object can then be used to generate a report as described in https://istanbul.js.org/docs/advanced/coverage-object-report/
d9e0ebe
to
8e465a4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@divdavem this PR looks incomplete.
I understand the part where it allows instrumenting the code in builds in general, but it doesn't actually do anything with that instrumentation (e.g. using that PR and running ng e2e --code-coverage
does not give me any coverage over end-to-end tests).
It is unclear to me how it is meant to be used. There are also no tests, which are required for new features.
Before investing more time in this feature perhaps it would be better to discuss the general with which you want to tackle this feature, so we can come to an agreement before developing it.
@filipesilva Thank you for your answer. I am glad that a human (and not only bots) looked at my PR!
I explained in this comment of #6286 how this new feature can be used with e2e tests:
The simple change that I did in this PR simplifies a lot the current steps (detailed in https://github.com/edvlucas/angular-e2e-coverage) that have to be done to instrument the code to get a coverage report for e2e tests. More generally, I think that being able to produce instrumented code that can be used just like non-instrumented code but also fills a
I will add some tests.
If you think it is required to include, as part of this PR, the production of the coverage report for e2e tests (instead of only the instrumentation), we indeed, as you said, have to discuss how to best implement the collection of coverage results. Please tell me what you think. |
Thanks for explaining the usage. My worry with this usage pattern is that adding the This, I feel, is not ideal. A users expectation for And at this point it's mostly a custom webpack config change, akin to what https://github.com/manfredsteyer/ngx-build-plus is usually used for. It's not trivial to create a robust user journey for this feature though. Like you mentioned, it would require wrapping the users protractor config, and to allow for multiple testing pattern (multiple page open/navigation/etc). Let me bring this up with team to see what they think. |
Thank you for your answer. We could also add something like the following async onComplete() {
const coverageData = await browser.executeScript('return JSON.stringify(window.__coverage__);');
if (coverageData) {
const { createReporter: createCoverageReporter } = require('istanbul-api');
const { createCoverageMap } = require('istanbul-lib-coverage');
const coverageMap = createCoverageMap(JSON.parse(coverageData));
const reporter = createCoverageReporter();
reporter.dir = require('path').join(__dirname, 'coverage-e2e');
reporter.addAll(['json', 'lcov', 'text'])
reporter.write(coverageMap);
}
} This would automatically generate the report in the |
Thank you for the contribution! We discussed this feature today. Although we think it's out of the scope of the CLI at this point, we believe it could be beneficial for our users. I'd recommend you to look at @manfredsteyer's ngx-build-plus and consider letting folks take advantage of e2e test coverage through a plugin. |
@divdavem I need this feature, my company is using cypress and I need a way to extract the coverage, is your feature implemented in a package or something? |
@beckmx I did not implement the feature in a package. You can have a look at this PR I did on ng-bootstrap for an example on how to implement it on an application. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This PR is related to #6286 and allows
--code-coverage
and--code-coverage-exclude
options to be used withng build
,ng serve
andng e2e
, so that it is easy to instrument the code for coverage.When used with these commands, the
--code-coverage
switch does not automatically create a file with the report but allows the developers to access the__coverage__
variable from Istanbul.js inside the browser to get coverage data.That object can then be used to generate a report as described in https://istanbul.js.org/docs/advanced/coverage-object-report/