【问题标题】:Why is my NYC Code coverage not working with ESM?为什么我的 NYC 代码覆盖范围不适用于 ESM?
【发布时间】:2021-10-30 13:28:46
【问题描述】:

我有以下代码...

App.mjs

import express from "express";
const port = process.env.PORT || 4000;

class CommunicationsApplication{
  constructor() {
    this.app = CommunicationsApplication.getExpress();
    this.app.get('/', (req, res) => {
      res.send('Hello World!')
    })

    this.app.listen(port, () => {
      console.log(`listening at http://localhost:${port}`)
    })
  }
  static getExpress(){
    return express()
  }
}

App.spec.mjs

import {CommunicationsApplication} from "./App.mjs";
import sinon from "sinon";
import {expect} from "chai";

it('Testing to see if test works', () => {
  const result = {
    get: sinon.fake(),
    listen: sinon.fake()
  }
  sinon.stub(CommunicationsApplication, 'getExpress').callsFake(()=>result)
  new CommunicationsApplication();
  expect(result.get.callCount).to.eq(1);
  expect(result.listen.callCount).to.eq(1);
})

package.json

"type": "module",
...
"test": "nyc mocha --recursive './lib/**/*.spec.mjs' --require esm"

但是当我跑步时,我看到...

  ✔ Testing to see if test works

为什么我的代码覆盖率不起作用?

  1 passing (4ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

【问题讨论】:

    标签: istanbul ecmascript-next es6-modules


    【解决方案1】:

    不会接受这个答案,但这确实有效......

    "test": "c8 mocha --recursive './lib/**/*.spec.mjs' --require esm"
    

    【讨论】:

      【解决方案2】:

      NYC 尚不支持 Node 原生 ESM。见https://github.com/istanbuljs/nyc/issues/1287

      【讨论】:

        猜你喜欢
        • 2013-06-03
        • 2019-11-07
        • 2020-08-13
        • 2018-05-21
        • 2018-09-13
        • 2016-03-31
        • 2020-04-19
        • 2017-07-21
        • 1970-01-01
        相关资源
        最近更新 更多