PS: Now I recommend using a single jest instead of mocha / instanbul / nyc / chai / etc.
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:
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