Search code examples
node.jsmocha.jsnyc

nyc mocha is generating nothing despite providing nyc config to package.json


I'm having a bit of trouble using nyc + mocha. My tests are passing but I get no reports, despite adding a nyc configuration object to my package json:

//package.json
{
  "name": "open-concepts",
  "version": "1.0.0",
  "description": "Simply matching idea makers and doers",
  "main": "app.js",
  "scripts": {
    "start": "nodemon --watch './**/*.ts' --exec 'ts-node-esm' app.ts",
    "test": "NODE_ENV='test' nyc mocha -r ts-node/register 'test/**/*.*.*.ts' --exit",
   },
"nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "check-coverage": true,
    "all": true,
    "include": [
      "test/**/*.*.[tj]s?(x)"
    ],
    "reporter": [
      "html",
      "lcov",
      "text",
      "text-summary",
      "cobertura"
    ],
    "report-dir": "coverage"
  }
}

When I run the tests, I get this error:

 91 passing (4s)

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

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================
runner.once is not a function <-- I don't know where this comes from...

Am I missing something in my nyc configuration?

TIA!


Solution

  • I believe the issue is your include statement:

        "include": [ "test/**/*.*.[tj]s?(x)" ]
    

    This should correspond with the src files you want to receive coverage for, not the test files themselves. You'd probably want to exclude the test files from the coverage report.

    The nyc config I use in many places is:

    "nyc": {
        "all": true,
        "extension": [
          ".ts",
          ".tsx"
        ],
        "exclude": [
          "**/*.d.ts",
          "**/*.js",
          "**/*.spec.ts"
        ],
        "reporter": [
          "text",
          "html",
          "cobertura"
        ],
        "require": ["ts-node/register"]
      }
    

    All .ts or .tsx files get included via the extension param, then the exclude filters our the test files, some of the generated ts files as well as any JS.

    If you're only looking for JS file coverage, switch up the extension param accordingly.