I was seeking answer for the same scenario for a while but there is no straight solution to instrument bundles generated through angular cli build job(ng build).
There are two ways to achieve this -
- We can use webpack config of
istanbul-instrumenter-loader using customWebpackConfig- suitable if you are using Angular 8.
- We can generate
webpack by (by ng eject) then we can achieve this with help of webpack plugin - istanbul-instrumenter-loader. - suitable if you are using Angular 5. In Angular CLI 6 ng eject-command has been removed, and it is not coming back.
Steps:-
- Instrument user bundle using
customWebpackConfig or webapck & istanbul-instrumenter-loader
- ref- http://opensourceforgeeks.blogspot.com/2018/12/how-to-customize-build-configuration.html
- ref- https://webpack.js.org/loaders/istanbul-instrumenter-loader/#with-babel
- Run your e2e/IT test suit on the instrumented bundle
- Collect your istanbul coverage(
window.__ coverage__) json from browser context & save it as coverage.json file.
- Genarate viewable report using istanbul comments
istanbul report(istanbul should be installed globally),It will generate coverage/lcov-report folder with html files to view the code coverage report for each files.