Launch Mocha + Istanbul + Babel

I am having problems running istanbul with mocha and babel compiler.

all my tests run just fine, but after all the tests I have done, he shows me this message:

No coverage information was collected, exit without writing coverage information

And he does not create a coverage report.

The command in which Im works:

NODE_ENV=test istanbul cover _mocha -- --require babel-core/register --recursive

the project is posted on github: https://github.com/weslleyaraujo/react-flux-puzzle/tree/feat/unit-tests-24

Any ideas what this could be?

+44
babeljs mocha istanbul
Nov 10
source share
3 answers

Using Babel 6.x, let's say we have the test/pad.spec.js :

 import pad from '../src/assets/js/helpers/pad'; import assert from 'assert'; describe('pad', () => { it('should pad a string', () => { assert.equal(pad('foo', 4), '0foo'); }); }); 

Set a bunch of crap:

 $ npm install babel-istanbul babel-cli babel-preset-es2015 mocha 

Create .babelrc :

 { "presets": ["es2015"] } 

Run the tests:

 $ node_modules/.bin/babel-node node_modules/.bin/babel-istanbul cover \ node_modules/.bin/_mocha -- test/pad.spec.js pad ✓ should pad a string 1 passing (8ms) ============================================================================= Writing coverage object [/Volumes/alien/projects/forked/react-flux-puzzle/coverage/coverage.json] Writing coverage reports at [/Volumes/alien/projects/forked/react-flux-puzzle/coverage] ============================================================================= =============================== Coverage summary =============================== Statements : 100% ( 4/4 ) Branches : 66.67% ( 4/6 ), 1 ignored Functions : 100% ( 1/1 ) Lines : 100% ( 3/3 ) ================================================================================ 

UPDATE . I have had success using nyc (which istanbul consumes) instead of istanbul / babel-istanbul . This is somewhat less complicated. To try:

Install the material (you can remove babel-istanbul and babel-cli ):

 $ npm install babel-core babel-preset-es2015 mocha nyc 

Create .babelrc as above.

Do the following:

 $ node_modules/.bin/nyc --require babel-core/register node_modules/.bin/mocha \ test/pad.spec.js 

... which should give you similar results. By default, it puts coverage information in .nyc-output/ and prints a nice summary of the text in the console.

Note. You can remove node_modules/.bin/ from any of these commands when placing the command in the package.json scripts field.

+56
Nov 15 '15 at 21:07
source share


PS: Now I recommend using a single jest instead of mocha / instanbul / nyc / chai / etc.




Solution A: Using nyc and babel-plugin-istanbul

Customization (don't forget @next for nyc ) :

 npm install --save-dev nyc babel-plugin-istanbul babel-register 

Add env to babel config:

 { "env": { "nyc": { "plugins": ["istanbul"] } } } 

nyc config:

 { "reporter" : ["text", "text-summary", "lcov", "html"], "include" : ["src/**/*.js"], "require" : ["babel-register"], "sourceMap" : false, "instrument" : false, "all" : true } 

PS: include field must be specified in .nycrc in package.json , if specified on the command line, coverage will not work

Running tests:

 # 1. Build NODE_ENV=nyc babel src --out-dir lib # 2. Coverage nyc mocha 



Solution B: No additional packages: only basic

Recently work has been done on istanbul ( 1.0.0-alpha.2 ) to support the generated Babel code with source maps (see # 212 and this one for an example).

There are two ways:

  • A. Tests written against previously converted code.
  • B. Tests written against source code and passed together in memory at run time


B1. Tests that export (previously) passed code

This is done in 2 stages: first, create your source using babel (for example, from. / Src to. / Out) and write your tests against a crowded source ( export foo from "./out/foo"; ).

Then you can run the tests using istanbul 1.0.0-alpha.2 :

 istanbul cover _mocha -- ./test --compilers js:babel-register 

Now, if you want the code coverage to correspond to the source code that you wrote (and not transferred), be sure to create using the source maps babel options set to and :

 babel ./src --out-dir ./out --source-maps both 

PS: If necessary, you can also do:

 istanbul cover _mocha -- ./test --compilers js:babel-register \ --require babel-polyfill \ --require should \ --require sinon 


IN 2. Tests directly exporting source code

In this case, you write your tests against the original source ( export foo from "./src/foo"; ), and without further steps, you directly run istanbul 1.0.0-alpha.2 using babel-node against cli.js:

 babel-node ./node_modules/istanbul/lib/cli.js cover _mocha -- ./test 

PS: If necessary, you can also do:

 babel-node ./node_modules/istanbul/lib/cli.js cover _mocha -- ./test --require babel-polyfill \ --require should \ --require sinon 
+21
Mar 13 '16 at 21:43
source share

Currently, this coverage report material is still a bit dirty on 04/17/2016, and with my React project, which has .jsx files and a supporting file, the coverage report for the script is as follows:

 istanbul cover node_modules/mocha/bin/_mocha -- \ --compilers js:babel-core/register \ --require ./test/testhelper.js \ \"test/**/*@(.js|.jsx)\" 

So it would not be easy for the current version 0.4.3 of Istanbul not to work with Babel, so you need to use the experimental alpha version:

 npm install istanbul@1.0.0-alpha.2 --save-dev 

And then you need .istanbul.yml -file so that .jsx files with these lines are recognized in Istanbul:

 instrumentation: root: . extensions: ['.js', '.jsx'] 

And now that should work. Also, as a small bonus, if you want to add coverage reports using Travis and Coveralls, you need to:

  • include the project in https://coveralls.io
  • add jumpsuits npm i coveralls --save-dev
  • add this to your .travis.yml :

     script: - npm --silent test - cat ./c coverage/lcov.info | coveralls 

And now you can put this cool icon in your README. Neato!

+4
Apr 17 '16 at 15:58
source share



All Articles