Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Code coverage for Angular 2 scaffold #503

Closed
ManuelDeLeon opened this issue Dec 9, 2016 · 2 comments
Closed

Code coverage for Angular 2 scaffold #503

ManuelDeLeon opened this issue Dec 9, 2016 · 2 comments

Comments

@ManuelDeLeon
Copy link

You might want to consider adding code coverage to the Angular 2 scaffold.

Here's how I did it:

1) Modify package.json
1.1) Add the following to devDependencies:

    "http-server": "^0.9.0",
    "karma-remap-istanbul": "^0.2.1",
    "sourcemap-istanbul-instrumenter-loader": "^0.2.0"
  • http-server to easily show the generated html files. Running npm run coverage is easier than navigating to the root html file of the code coverage result.

  • karma-remap-istanbul to generate the report.

  • sourcemap-istanbul-instrumenter-loader to show original TypeScript files, not the transpiled ones.

    1.2) Add the scripts to show the code coverage (the report is generated when you run the tests).

  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js",
    "test-single": "karma start ClientApp/test/karma.conf.js --single-run",
    "show-coverage": "npm run test-single && http-server -c-1 -o -p 9875 ClientApp/test/coverage"
  },

2) Modify ClientApp/test/boot-tests.ts to load all .ts files, othewise it will only show the code coverage for the components touched by test files.

const context = require.context('../app', true, /\.ts$/);

3) Modify ClientApp/test/karma.conf.js to generate the code coverage:
3.1) Update webpack config to use sourcemap-istanbul-instrumenter-loader:

var path = require('path');
var webpackConfig = require('../../webpack.config.js').filter(config => config.target !== 'node')[0];
webpackConfig.module.postLoaders = [{
    test: /\.ts$/,
    include: [path.resolve(__dirname, "../app")],
    loader: 'sourcemap-istanbul-instrumenter-loader?force-sourcemap=true',
    exclude: [/\.spec\.ts$/]
}];

3.2) Add the reporter and use our modified webpack config:

webpack: webpackConfig,
reporters: ['progress', 'karma-remap-istanbul'],
remapIstanbulReporter: {
    reports: {
        html: 'ClientApp/test/coverage'
    }
}

4) Update .gitignore so it ignores the coverage results:

/ClientApp/test/coverage/**

Result:

image

image

Sample repo:

https://github.com/ManuelDeLeon/jss-angular-code-coverage

Hope it helps.

@SteveSandersonMS
Copy link
Member

Thanks for the suggestion! For guides like this, I think the wiki in this repo would be an ideal home.

If you don't mind, I'll copy your guide to a page in that wiki (once GitHub's wiki system starts working again - it says it's currently down).

@SteveSandersonMS
Copy link
Member

Now added at https://github.com/aspnet/JavaScriptServices/wiki/Angular2SpaCodeCoverage

Hopefully we can get more recipes like that onto the wiki soon too!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants