diff --git a/.gitignore b/.gitignore index 4c75cea..301132b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules *.log package-lock.json dist -build \ No newline at end of file +build +coverage diff --git a/README.md b/README.md index 143bfc2..e494680 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Most importantly, Karmatic provides a (headless) browser test harness in a singl ## Installation ```sh -npm i -D karmatic +npm i -D webpack karmatic ``` ... then add a `test` script to your `package.json`: diff --git a/package.json b/package.json index 7ef8e04..8658716 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "dependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", + "babel-plugin-istanbul": "^5.1.0", "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-plugin-transform-react-jsx": "^6.24.1", "babel-polyfill": "^6.26.0", @@ -47,6 +48,7 @@ "jasmine-core": "^3.3.0", "karma": "^3.1.1", "karma-chrome-launcher": "^2.2.0", + "karma-coverage": "^1.1.2", "karma-jasmine": "^2.0.1", "karma-sourcemap-loader": "^0.3.7", "karma-spec-reporter": "0.0.32", diff --git a/src/cli.js b/src/cli.js index c67eeda..5d4e789 100644 --- a/src/cli.js +++ b/src/cli.js @@ -15,7 +15,8 @@ let prog = sade('karmatic'); prog .version(version) .option('--files', 'Minimatch pattern for test files') - .option('--headless', 'Run using Chrome Headless', true); + .option('--headless', 'Run using Chrome Headless', true) + .option('--coverage', 'Report code coverage of tests', true); prog .command('run [...files]', '', { default: true }) diff --git a/src/configure.js b/src/configure.js index 3e0c8b8..2e4818a 100644 --- a/src/configure.js +++ b/src/configure.js @@ -27,7 +27,9 @@ export default function configure(options) { 'karma-spec-reporter', 'karma-sourcemap-loader', 'karma-webpack' - ]; + ].concat( + options.coverage ? 'karma-coverage' : [] + ); const WEBPACK_CONFIGS = [ 'webpack.config.babel.js', @@ -109,7 +111,9 @@ export default function configure(options) { basePath: cwd, plugins: PLUGINS.map(require.resolve), frameworks: ['jasmine'], - reporters: ['spec'], + reporters: ['spec'].concat( + options.coverage ? 'coverage' : [] + ), browsers: [options.headless===false ? 'KarmaticChrome' : 'KarmaticChromeHeadless'], customLaunchers: { @@ -122,6 +126,14 @@ export default function configure(options) { } }, + coverageReporter: { + reporters: [ + { type: 'text-summary' }, + { type: 'html' }, + { type: 'lcovonly', subdir: '.', file: 'lcov.info' } + ] + }, + formatError(msg) { try { msg = JSON.parse(msg).message; diff --git a/src/lib/babel-loader.js b/src/lib/babel-loader.js index e6e5365..8129497 100644 --- a/src/lib/babel-loader.js +++ b/src/lib/babel-loader.js @@ -17,7 +17,9 @@ export default function babelLoader(options) { plugins: [ [require.resolve('babel-plugin-transform-object-rest-spread')], [require.resolve('babel-plugin-transform-react-jsx'), { pragma: options.pragma || 'h' }] - ] + ].concat( + options.coverage ? require.resolve('babel-plugin-istanbul') : [] + ) } }; }