I use mocha and isparta directly with Babel 6 as follows:
npm test command
BABEL_ENV=test babel-node node_modules/isparta/bin/isparta cover _mocha
.babelrc
{ "plugins": [ "add-module-exports", "transform-decorators-legacy", "transform-runtime" ], "presets": [ "es2015", "react", "stage-0" ], "env": { "test": { "plugins": [ "rewire" ] } } }
test/mocha.opts
--compilers js:babel-core/register --require test/init.js src*_test.js*
test.init.js
'use strict'; require('mock-require')('clappr'); require('testdom')('<html><body></body></html>', { React: 'react', localStorage: 'localStorage' });
.istanbul.yml
instrumentation: root: src excludes: ['*_test.js']
select package.json dependencies
"babel-cli": "^6.7.5", "babel-core": "^6.7.2", "babel-eslint": "^5.0.0", "babel-loader": "^6.2.4", "babel-plugin-add-module-exports": "^0.1.2", "babel-plugin-rewire": "^1.0.0-rc-2", "babel-plugin-runtime": "^1.0.7", "babel-plugin-syntax-jsx": "^6.5.0", "babel-plugin-transform-decorators-legacy": "^1.3.4", "babel-plugin-transform-runtime": "^6.6.0", "babel-preset-es2015": "^6.6.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.7.2", "babel-runtime": "^5.8.34", "babel-template": "^6.7.0", "babel-types": "^6.7.2", "isparta": "^4.0.0", "mocha": "^2.4.5",
Note on .JSX files
I renamed all my .JSX files to .JS, and here's why:
- I use codecov to report on hosted coverages. This showed that while
coverage/lcov-report/index.html shows the correct coverage, there is no .JSX file in the JSON coverage files. I could never figure it out. As far as I can tell, this is a mistake in isparta or istanbul. I also tried istanbul@1.0.0-alpha.2 and found that it has the same problem. - React is used to recommend naming .JSX files in the interest of using transformations and editors. This is no longer a recommendation. As far as I can tell, that no longer matters.
Since switching to .JS, I have not seen any problems with tools, including Atom and IntelliJ.
If you don't want to rename your files, you can add the following to my examples above:
- In the script, after
isparta cover , add --include \"src/**/*_test.jsx\" . - In
.istanbul.yml add
extensions: - .js - .jsx