nyc 代码覆盖不适用于 NodeJs Express 服务器

问题描述 投票:0回答:1

我有一个名为

app.js
的 NodeJs Express 服务器,我想使用
nyc
进行代码覆盖。

// app.js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send({
    message: 'Hello World!'
  })
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
//.nycrc.json
{
  "all": true,
  "cache": false,
  "check-coverage": true,
  "sourceMap": false,
  "instrument": false,
  "cmd": "./",
  "temp-dir": "./.nyc_output",
  "include": [
    "e2e-app/src/**/*.ts",
    "server/app.js"
  ],
  "exclude": [
    "e2e-app/src/**/*.spec.ts"
  ],
  "report-dir": "./coverage-e2e",
  "reporter": [
    "lcov",
    "text",
    "text-summary",
    "cobertura"
  ],
  "branches": 80,
  "lines": 80,
  "functions": 80,
  "statements": 80,
  "watermarks": {
    "statements": [
      70,
      100
    ],
    "branches": [
      70,
      100
    ],
    "functions": [
      70,
      100
    ],
    "lines": [
      70,
      100
    ]
  }
}
  1. 在终端我做
    nyc node server/app.js
  2. 在我的浏览器中,我转到
    http://localhost:3000
    ,我看到我的屏幕上打印了
    Hello World!
  3. 我看到有一个用
    .nyc_output
    文件创建的文件夹
    json
    ,但是 json 的内容只是一个空对象。
  4. 我杀死 NodeJs 进程,然后运行
    nyc report
    ,我得到一个空报告。

我做错了什么?

node.js express code-coverage istanbul nyc
1个回答
0
投票

解决方案是这样做

node node_module/nyc/bin/nyc npm start

© www.soinside.com 2019 - 2024. All rights reserved.