1

I have a NodeJs express server named app.js, I want to do a code coverage using 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. In terminal I do nyc node server/app.js
  2. In my browser, I go to http://localhost:3000, I see the Hello World! printed on my screen.
  3. I see there is a folder .nyc_output created with a json file, but the json's content is just an empty object.
  4. I kill NodeJs process, and run nyc report, I got an empty report.

What did I do wrong here?

Peter Huang
  • 972
  • 4
  • 12
  • 34
  • Are you using an [.nycrc](https://www.npmjs.com/package/nyc) file? Have you tried with `nyc --reporter=lcov`? – Pieterjan Apr 15 '23 at 00:05
  • @Pieterjan I updated the question with `.nycrc.json`, I tried to use `nyc --reporter-lcov`, the report is still empty. – Peter Huang Apr 15 '23 at 23:30

1 Answers1

0

The solution is do this node node_module/nyc/bin/nyc npm start

Peter Huang
  • 972
  • 4
  • 12
  • 34