-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
code coverage with karma-coverage #21
Comments
Sorry, I have no experience with karma-coverage. The only thing that I can tell is that |
I have tried something like this: files: [
'app/**/*.js',
'test/**/*.spec.js'
],
preprocessors: {
'app/**/*.js': [ 'webpack', 'coverage' ],
'test/**/*.spec.js': [ 'webpack' ]
}, and got Do I undertstand it right that every file in |
just made https://github.com/deepsweet/istanbul-instrumenter-loader to deal with it, works fine for me. |
Cool. Make it cachable: http://webpack.github.io/docs/how-to-write-a-loader.html#flag-itself-cacheable-if-possible |
done. |
karma-coverage and karma-webpack seem to be working together for me::
Setting the karma preprocessors to ["webpack", "coverage"] for the test files results in a coverage report using the compiled output. This might not be ideal, but it certainly works. |
It is working fine for me. But I have another problem. |
@deepsweet +1 man thats awesome |
i'd call this closed from my perspective. |
I consider this an issue also. I have a file I am testing that is about 10 lines and has only "node_modules" loaded. Coverage report shows "43.48% (4690 / 10787)" because it is checking the entire bundle. Do numbers show up? Yes. But I don't think that makes it a non-issue |
@joshq00 you need to split your single bundle so you can instrument only the piece you care about |
I've tinkered until I got a solution that works as expected. For those looking for how to accomplish this, here's what I did: Make a webpack test config file webpack.test.config.es6/*
...
your webpack test config
...
note: for babel-loader, use 'babel?optional=runtime'
*/
// add instanbul postloader
cfg.module.postLoaders = [
{
test: /\.(es6|jsx?)$/,
exclude: /(__tests__|node_modules|bower_components)/,
loader: 'istanbul-instrumenter'
}
]; Then use these settings in your karma configuration karma.conf.es6import webpack from './webpack.test.config.es6';
let files = [
'./node_modules/phantomjs-polyfill/bind-polyfill.js',
'./node_modules/babel-core/browser-polyfill.js',
{
pattern: 'src/**/__tests__/*-test.es6'
}
];
let preprocessors = {
'src/**/__tests__/*-test.es6': [ 'webpack' ]
};
let reporters = [ 'progress', 'coverage' ];
/*
...
the rest of your config
...
*/
export default function ( config ) {
config.set( {
files,
preprocessors,
webpack,
reporters,
// ... the rest of your cfg
} );
} |
try isparta-loader to cover an original ES6 code lines instead of transpiled ones. |
@joshq00 your example of configuration works fine, with a little problem. The only files the will be coverage are the one imported by the test files. What will give you a false report about your code base coverage. |
@pablolmiranda do you have meanwhile a working solution to share? |
I have a solution, but it is not the ideal. require('./support/es5_shim.js');
require('./support/factories');
require('./support/mock_context');
var testsContext = require.context("../src/app", true, /.*jsx$/);
testsContext.keys().forEach(testsContext); This will for the creation of the bundle including the tests if they are in the Inside Karma reference this file as a entry point:
I have this configuration for module and instrument the code in my Karma config file: module: {
loaders: [{
test: /\.jsx$/,
loaders: ['jsx-loader?insertPragma=React.DOM']
}],
postLoaders: [{ // << add subject as webpack's postloader
test: /\.jsx$/,
exclude: /(__tests__|node_modules|legacy)\//,
loader: 'istanbul-instrumenter'
}]
}, This will give the coverage all the components included in the bundle, excluding the tests and node modules. But even after that the coverage reports generates a bundle coverage. So I use shell script to remove this information from my var path = require('path'),
shell = require('gulp-shell'),
COVERAGE_FOLDER = 'coverage',
lcovFile = path.resolve(COVERAGE_FOLDER, 'lcov.info'),
jsLcovFile = path.resolve(COVERAGE_FOLDER, 'lcov-js.info');
module.exports = function(gulp) {
return function() {
return gulp.src(lcovFile, {read: false})
.pipe(shell([
'sed \'/test\\/test_ui.js/,/end_of_record/d\' <%= file.path %> > <%= bak(file.path) %>',
'sed \'1d\' <%= bak(file.path) %> > <%= file.path %>',
'rm <%= bak(file.path) %>',
'cat ' + jsLcovFile + ' >> <%= file.path %>',
'genhtml <%= file.path %> -o ' + COVERAGE_FOLDER + '/lcov-report'
], {
templateData: {
bak: function(fileName) {
return fileName + '.bak';
}
}
}));
};
}; This will open the As I said, it is not the ideal solution but works pretty good. My full Karma configuration for reference: var path = require('path'),
configSettings,
TEST_RESULTS_DIR = 'test-results',
COVERAGE_DIR = path.join(TEST_RESULTS_DIR, 'coverage');
configSettings = {
"normal": {},
};
module.exports = function(config) {
var srcFolder = path.join(__dirname, '..', 'src'),
distFolder = path.join(__dirname, '..', 'dist'),
supportFolder = path.join(__dirname, '..', 'test', 'support'),
tapResults = TEST_RESULTS_DIR + '/results.tap',
karmaConfig = {
browsers: ['PhantomJS'],
frameworks: ['mocha', 'chai-sinon', 'source-map-support'],
reporters: ['progress', 'tapFile', 'coverage'],
browserNoActivityTimeout: 60000,
files: [
'../test/test_ui.js'
],
preprocessors: {
'../test/test_ui.js': ['webpack', 'coverage']
},
coverageReporter: {
type: 'lcov',
dir: COVERAGE_DIR,
subdir: '.'
},
tapReporter: {
outputFile: tapResults,
suite: ''
},
proxies: {
'/test-thumb/': 'test/support/images/default_user.jpg',
'/images/': 'test/support/images/default_user.jpg'
},
webpack: Object.keys(configSettings).map(function(name) {
var config = {
name: name,
resolve: {
extensions: ["", ".js", ".jsx"]
},
modulesDirectories: [
'node_modules'
],
module: {
loaders: [{
test: /\.jsx$/,
loaders: ['jsx-loader?insertPragma=React.DOM']
}],
postLoaders: [{ // << add subject as webpack's postloader
test: /\.jsx$/,
exclude: /(__tests__|node_modules|legacy)\//,
loader: 'istanbul-instrumenter'
}]
},
externals: {
'nedb': 'NaN',
'faye-websocket': 'NaN'
},
noParse: [
/node_modules/
],
node: {
net: "empty",
tls: "empty"
},
devtool: 'inline-source-map'
};
Object.keys(configSettings[name]).forEach(function(key) {
config[key] = configSettings[name][key]
});
return config;
}),
};
config.set(karmaConfig);
}; |
@pablolmiranda Thanks a lot! |
I created a karma plugin called karma-sourcemap-writer to generate source map file of the test bundle, and used remap-istanbul to map coverage info to it's source and it works great. Hope this tool helps! |
coveralls doesn t play well with karma-webpack codymikol/karma-webpack#21
@jas-chen I seem to currently be unable to get it to work... I do get the overview with the right percentages (weird), but my karma errors as it cannot find the sourcemap to the tested files [src/..] and therefore I cannot click the percentages to go down into the file and see what lines are covered. Kind regards, |
If anybody is stumbling upon this thread, it's worth mentioning that babel-plugin-istanbul is a new option that offers a painless way to instrument ES6 sources with Istanbul. If you're already using Babel in your Webpack configuration, it is extremely straightforward to set up and use. I spent a lot of time fiddling with the other options presented earlier in this thread. Compared to the other options, the Babel plugin is (by far) the easiest to configure, and produces the most accurate coverage reports. If you're already using babel, just In For example, my configuration looks like this:
Hope this helps anybody else caught in this rabbit-hole! |
I am also using babel-plugin-istanbul, and it works great for ES6 files, but not for typescript files, which we also have in our app. Does anybody know about a good solution to have code coverage for both? |
I create a new plugin reporter base on new Istanbul API v1 and Feedback are welcome. It relay on |
@alinaMihai Just curious what your workflow is that involves TS and Babel, but not together. |
@schmod We are using webpack with ts-loader and babel for ts files and babel for js files. In babelrc I have added the istanbul plugin for the test environment. I have found this flow on a stackoverflow question, but I am not sure how to apply it:
I hope I've been more clear about the issue :) |
I know the folk at https://github.com/preboot/angular2 webpack got it setup, you may wanna check there |
@MikaAK I think the markdown broke your link. Did you mean this? |
yep that one! |
Having some trouble configuring karma coverage with webpack. The testing itself is working great. Here's my attempted config:
// Karma configuration
// Generated on Sat Apr 19 2014 15:17:46 GMT-0500 (CDT)
var path = require('path'),
webpack = require('webpack');
module.exports = function(config) {
config.set({
};
Any ideas?
The text was updated successfully, but these errors were encountered: