Code coverage using Istanbul for Ember apps.
- If using Mocha, Testem
>= 1.6.0for which you need ember-cli> 2.4.3 - If using Mirage you need
ember-cli-mirage >= 0.1.13 - If using Pretender (even as a dependency of Mirage) you need
pretender >= 0.11.0 - If using Mirage or Pretender, you need to set up a passthrough for coverage to be written.
ember-cli-babel >= 6.0.0
ember install ember-cli-code-coverage
Coverage will only be generated when an environment variable is true (by default COVERAGE) and running your test command like normal.
For example:
COVERAGE=true ember test
If you want your coverage to work on both Unix and Windows, you can do this:
npm install cross-env --save-dev
and then:
cross-env COVERAGE=true ember test
Coverage also works when running tests in parallel, eg:
COVERAGE=true ember exam --split=2 --parallel=true
Configuration is optional. It should be put in a file at config/coverage.js (configPath configuration in package.json is honored). In addition to this you can configure Istanbul by adding a .istanbul.yml file to the root directory of your app (See https://github.com/gotwarlost/istanbul#configuring)
-
coverageEnvVar: Defaults toCOVERAGE. This is the environment variable that when set will cause coverage metrics to be generated. -
reporters: Defaults to['lcov', 'html']. Thejson-summaryreporter will be added to anything set here, it is required. This can be any reporters supported by Istanbul. -
excludes: Defaults to['*/mirage/**/*']. An array of globs to exclude from instrumentation. Useful to exclude files from coverage statistics. -
coverageFolder: Defaults tocoverage. A folder relative to the root of your project to store coverage results.
module.exports = {
coverageEnvVar: 'COV'
}To work, this addon has to post coverage results back to a middleware at /write-coverage.
If you are using ember-cli-mirage you should add the following:
// in mirage/config.js
this.passthrough('/write-coverage');
this.namespace = 'api'; // It's important that the passthrough for coverage is before the namespace, otherwise it will be prefixed.
If you are using ember-cli-pretender you should add the following:
// where ever you set up the Pretender Server
var server = new Pretender(function () {
this.post('/write-coverage', this.passthrough);
});
You can also test for code coverage goals with the included command ember test-code-coverage.
It takes the following parameters:
- target-lines: The target percentage for lines covered
- target-functions: The target percentage for functions covered
- target-statements: The target percentage for statements covered
- target-branches: The target percentage for branches covered
For example: ember test-code-coverage -target-lines=80 -target-branches=85
You can also configure those in the config/coverage.js file, in their camel cased form, like this:
module.exports = {
targetLines: 80,
targetFunctions: 85.5,
// Other configuration
coverageEnvVar: 'COV'
}These values will then be the defaults when running ember test-code-coverage.
Running the command will output something like this to your console:
+ Lines covered: 60.00% 69.17%
+ Functions covered: --.--% 72.18%
+ Statements covered: --.--% 68.93%
- Branches covered: 70.00% 60.16%
- Test coverage check failedFor any type of check where no target is specified, it will simply output the actual value without doing any comparison.
This addon was inspired by ember-cli-blanket.
The primary differences are that this addon uses Istanbul rather than Blanket for coverage and it instruments your application code as part of the build, when enabled.