gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 785 forks source link

Running istanbul/isparta within a compiled webpack bundle in node #470

Open johanneslumpe opened 8 years ago

johanneslumpe commented 8 years ago

Use case: a universal app, bundled with webpack, which uses style loaders etc. needs to be precompiled with webpack in order to replace the loaders so the code runs in node.

Using karma here is not a solution because the coverage also has to cover server-only code, which cannot run in the browser. I do not want to have 2 coverage reports. Is there any way to get this working?

I used isparta-instrumenter-loader for webpack to instrument the files. The issue I am having is to figure out how to now collect the coverage. Since webpack uses its own require function I cannot just prepend the hookRequire call at the top of the file.

The test run fine, but no coverage is ever collected.

Has anybody come across this already and found a feasible solution? Any help is greatly appreciated.

faergeek commented 8 years ago

You just need to collect separate json files for node and for karma and then you can merge them with istanbul report command. I could provide an example if needed.

johanneslumpe commented 8 years ago

@faergeek that does not help me I guess. Since my server code has to run the client code too. It is a universal application nonetheless.

I got a hacky solution done where I just manually remove styles includes from the source, but it definitely is not perfect.

You are suggesting to only run server tests in node and client tests in karma, right? That isn't feasible, since I have to guarantee that the client code runs fine in node too. Hence my server tests run both client+server tests (again, it's a universal app)

With your solution, how would I include all sources, even untested ones, in order to get the correct coverage? As soon as I require the client files with style imports in node, it breaks, if not compiled with webpack before loading.

faergeek commented 8 years ago

I've got a hacky solution which works a bit strangely, but better than nothing :-) How do you run your tests for both server and client?

johanneslumpe commented 8 years ago

I have a gulp task set up for that reads all source files, modifies the source to strip out all style requires and pipes those to the instrumenter and then to gulp-istanbul's hookRequire. After that I read all test files and pipe them to mocha and then write the report.

That works for now, since I have nothing else in the code except those styles requires. I have to set my NODE_PATH though, because I configured webpack to allow me to do root-relative requires.

faergeek commented 8 years ago

Hmm, actually it doesn't fully work for me. I thought that it works, because I just started setting it up and I've got just one test which requires sass.

It seems like it can't map from sources to original files after running tests bundle.

@douglasduteil Could you help us with that, please?

Would be cool to merge isparta into istanbul eventually BTW ;-)

faergeek commented 8 years ago

@johanneslumpe Did you try to use that fork instead of isparta? https://github.com/ambitioninc/babel-istanbul

johanneslumpe commented 8 years ago

@faergeek I did not, but that does not solve the actual issue I am having. The issue is not ES6/7 - the issue is using webpack loaders, which node does not understand and thus needing a precompile step to mark those loaders as a no-op on the server.

faergeek commented 8 years ago

Sorry, I just thought it have support for source maps, it could help in this situation. @gotwarlost Do you need some help to implement source map support? I saw source-map branch in repo :-)

faergeek commented 8 years ago

@johanneslumpe I think you should take a look at this thread https://github.com/gotwarlost/istanbul/issues/212

niftylettuce commented 8 years ago

Can't get this to work...

./node_modules/.bin/babel-node -r node_modules/.bin/isparta cover --report html --report text --report lcov ./node_modules/.bin/_mocha -- tests/**/*.test.js
Transformation error; return original code
{ [Error: Line 1: Unexpected token ILLEGAL]
  lineNumber: 1,
  description: 'Unexpected token ILLEGAL',
  index: 0 }
faergeek commented 8 years ago

@niftylettuce Are you sure that this error really breaks something? It's usually ok with mocha and isparta and everything works actually.

gotwarlost commented 8 years ago

@johanneslumpe - would it be possible to create a simple test app with its build script so I can try and figure out a solution? I'm currently working on istanbul v1 and would like to support this use case in a general way. Thanks.

johanneslumpe commented 8 years ago

@gotwarlost I will see if i can get around to extracting a minimal working example! I will report back here once that's done.

niftylettuce commented 8 years ago

I don't have any issues anymore.

niftylettuce commented 8 years ago

See how I did it with Frisbee https://github.com/niftylettuce/frisbee