webpack-contrib / istanbul-instrumenter-loader

Istanbul Instrumenter Loader
MIT License
273 stars 65 forks source link

Tests in same directory as source incorrect coverage #24

Closed mderrick closed 8 years ago

mderrick commented 8 years ago

I had coverage set up just fine when my tests were in their own directory but I've now moved tests alongside my source code and the coverage is all wrong.

I have done this:

const testsContext = require.context('../../src', true, /\.test\.js$/);
testsContext.keys().forEach(testsContext);

and have verified that the keys are only of.test.js files yet coverage is still reporting on the test files themselves. Would you happen to know why this happens? Is it an issue with require.context, the loader or Istanbul itself? Thanks!

nkbt commented 8 years ago

You need to exclude test files. There is a CLI option for that. I can check one of my projects for the concrete example later On Wed, 18 May 2016 at 22:06, Matt Derrick notifications@github.com wrote:

I had coverage set up just fine when my tests were in their own directory but I've now moved tests alongside my source code and the coverage is all wrong.

I have done this:

const testsContext = require.context('../../src', true, /.test.js$/); testsContext.keys().forEach(testsContext);

and have verified that the keys are only of.test.js files yet coverage is still reporting on the test files themselves. Would you happen to know why this happens? Is it an issue with require.context, the loader or Istanbul itself? Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/deepsweet/istanbul-instrumenter-loader/issues/24

nkbt commented 8 years ago
babel-node $(npm bin)/isparta cover \
  --report text --report lcov --verbose \
  --dir ./coverage  --include '**/!(*-test).js' test/index.js

so you can specify what files you include with --include '**/!(*-test).js'

mderrick commented 8 years ago

Thanks for the quick reply. It took me a while to work out was going on but I essentially did what you said by excluding .test.js from isparta. Part of my confusion was not knowing quite sure who was responsible for the coverge, istanbul or isparta. Honestly I'm still not sure.....

{
    test: /^((?!test).)*\.js$/,
    include: path.join(__dirname, '../../src'),
    loader: 'isparta'
}

Thanks!

nkbt commented 8 years ago

Isparta is just es6 wrapper for istanbul and runs it, so supports pretty much all CLI arguments.