4

我正在使用mocha单元测试我babel也在使用的 js 代码。我nyc用来做报道。但最终,结果显示为空。

bash 输出

为什么它显示 All files 0 即使我的测试文件需要redux.jsnumber-reducer.jsfiles ?

我在 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);
        });
...
    });
});
4

0 回答 0