Skip to content

Commit fb6c462

Browse files
committed
chore(*): add test-coverage on es6 source code
Since es6+ and JSX are transpiled, regular code coverage reports would be done on transpiled code. The previous way to to test coverage on original es6 source code was through isparta (which has been flagged as "No maintenance intended" by its maintainer). The best way now is to use babel-plugin-__coverage__ (directly a babel loader): https://github.com/dtinth/babel-plugin-__coverage__ The combo is: * babel-plugin-__coverage__ to instrument the es6 code while transpilation and add meta data that will be used by karma-coverage https://github.com/dtinth/babel-plugin-__coverage__ * karma-coverage to process this meta data and make reports (in html or for tools like jenkins, coveralls ...) https://github.com/karma-runner/karma-coverage Special thanks to @douglasduteil for https://github.com/douglasduteil/isparta which has been helpful.
1 parent 5016ef2 commit fb6c462

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// only enable it when process.env.NODE_ENV is 'development' or undefined
66
"development": {
77
"presets": ["react-hmre"]
8+
},
9+
// configuration for babel-plugin-__coverage__ - see https://github.com/dtinth/babel-plugin-__coverage__#readme
10+
// only enables the coverage loader when process.env.NODE_ENV=mock (used by karma-coverage to create coverage reports)
11+
// I use "mock", you might use "test"
12+
"mock": {
13+
"plugins": [ [ "__coverage__", { "ignore": "src/**/*.spec.js" } ] ]
814
}
915
}
1016
}

karma.conf.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
// retrieve args
2+
const argv = require('minimist')(process.argv.slice(2));
3+
const COVERAGE = argv.coverage === true;
4+
5+
// the following env vars are used in webpack.config.js
6+
process.env.UNIT_TEST = true;
7+
18
const path = require('path');
29
const webpackConfig = require('./webpack.config');
10+
const log = require('npmlog');
11+
log.level = 'silly';
312

4-
process.env.UNIT_TEST = true;
13+
const reporters = ['mocha'];
14+
const coverageReporter_reporters = [
15+
{type: 'lcov', dir: './build/reports/coverage'}
16+
];
17+
18+
if (COVERAGE) {
19+
log.info('karma', 'COVERAGE mode enabled');
20+
reporters.push('coverage');
21+
}
522

623
module.exports = function(config) {
724
config.set({
@@ -38,7 +55,8 @@ module.exports = function(config) {
3855
'karma-mocha-reporter',
3956
'karma-sourcemap-loader',
4057
'karma-chrome-launcher',
41-
'karma-phantomjs-launcher'
58+
'karma-phantomjs-launcher',
59+
'karma-coverage'
4260
],
4361

4462

@@ -47,7 +65,10 @@ module.exports = function(config) {
4765
// presets: ['airbnb']
4866
// }
4967
//},
50-
reporters: ['mocha'],
68+
coverageReporter: {
69+
reporters: coverageReporter_reporters
70+
},
71+
reporters: reporters,
5172
port: 9876,
5273
colors: true,
5374
logLevel: config.LOG_INFO,

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"postinstall": "npm run webdriver-manager-update",
99
"test": "npm run lint && npm run karma",
1010
"test-build": "./bin/test-build.sh",
11-
"karma": "npm run karma-watch -- --single-run --browsers PhantomJS",
11+
"karma": "npm run karma-watch -- --single-run",
1212
"karma-watch": "NODE_ENV=mock LINTER=false ./node_modules/karma/bin/karma start",
13+
"karma-coverage": "npm run karma -- --coverage",
1314
"mocha": "NODE_ENV=mock mocha 'src/**/*.spec.js' --compilers js:babel-core/register --recursive --require ./test/unit/setup.js",
1415
"mocha-watch": "npm run mocha -- --watch",
1516
"unit-test": "echo 'Deprecated, please use \"npm run karma\"' && exit 0",
@@ -58,6 +59,7 @@
5859
"babel-core": "^6.7.4",
5960
"babel-eslint": "^5.0.0",
6061
"babel-loader": "^6.2.4",
62+
"babel-plugin-__coverage__": "^0.11111.0",
6163
"babel-plugin-add-module-exports": "^0.1.2",
6264
"babel-plugin-transform-class-properties": "^6.6.0",
6365
"babel-plugin-transform-es2015-destructuring": "^6.6.5",
@@ -85,6 +87,7 @@
8587
"karma": "^0.13.22",
8688
"karma-babel-preprocessor": "^6.0.1",
8789
"karma-chrome-launcher": "^0.2.3",
90+
"karma-coverage": "^0.5.5",
8891
"karma-mocha": "^0.2.2",
8992
"karma-mocha-reporter": "^2.0.0",
9093
"karma-phantomjs-launcher": "^1.0.0",

0 commit comments

Comments
 (0)