我正在使用mocha
单元测试我babel
也在使用的 js 代码。我nyc
用来做报道。但最终,结果显示为空。
为什么它显示 All files 0 即使我的测试文件需要redux.js
和number-reducer.js
files ?
我在 package.json 中的测试脚本是
{
...
"scripts": {
...
"test": "cross-env NODE_ENV=test npm run lint && nyc --reporter=lcov --reporter=text mocha",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
...
"devDependencies": {
"@babel/core": "~7.3.4",
"@babel/preset-env": "~7.3.4",
"@babel/register": "~7.0.0",
"babel-loader": "~8.0.5",
"babel-plugin-istanbul": "~5.1.1",
"babel-polyfill": "~6.26.0",
"chai": "~4.2.0",
...
"cross-env": "~5.2.0",
"eslint": "~5.15.0",
"mocha": "~6.0.0",
"nyc": "~13.3.0",
...
"webpack": "~4.29.4",
"webpack-cli": "~3.2.1"
},
"nyc": {
"require": [
"@babel/register"
],
"reporter": [
"lcov",
"text"
],
"sourceMap": false,
"instrument": false
}
}
我的.babelrc
文件是
{
"presets": [
"@babel/preset-env"
],
"env": {
"test": {
"plugins": ["istanbul"]
}
}
}
我的test.spec.js
文件是
import chai from "chai";
let redux = require("../src/js/redux").redux;
let NumberReducer = require("../src/js/number-reducer").NumberReducer;
const expect = chai.expect;
describe("ReduxModule", function () {
it('should expect an object', function () {
const ReduxModule = redux(NumberReducer);
expect(ReduxModule).to.be.a("object");
});
describe('ReduxModule#getState', function () {
it('should give me zero', function () {
const ReduxModule = redux(NumberReducer);
expect(ReduxModule.getState()).to.equal(0);
});
...
});
});