nyc: NYC output is empty with mocha installed globally

Extracted from #822 I experience no coverage output when both nyc and mocha installed globally (in fact, only mocha installation plays role). Reproducible repo is here Env: Windows 7 Node 10.15.1 Nyc 13.3.0 Mocha 6.0.2

D:\Projects\nyc-test>nyc mocha


  √ should have tests

  1 passing (39ms)

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

D:\Projects\nyc-test>npm i mocha

(...) skipped 

D:\Projects\nyc-test>nyc node_modules\.bin\mocha

  √ should have tests

  1 passing (39ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 stub.js  |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 27 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Same issue , While Running nyc mocha
image

But while Running nyc node_modules/.bin/mocha image

``

After maaaany hours of debugging I finally found the problem. It’s the cmd and shell files and spawn-wrap.

They check if the node executable is in the same directory as they are, when they are installed as modules they work because there’s no node executable there and it then relies on using the one in the PATH, but it’s there when installed globally.

That’s not a problem on Unix because it uses a symbolic link to the JS file. Unless you use a Windows installation on Unix, then you have the same bug.

My solution right now is to patch spawn-wrap with this:

diff --git a/index.js b/index.js
index 908be7f..306b5ce 100644
--- a/index.js
+++ b/index.js
@@ -147,6 +147,21 @@ function setup(argv, env) {
     fs.writeFileSync(path.join(workingDir, cmdname), shim)
     fs.chmodSync(path.join(workingDir, cmdname), '0755')
   }
+  if (cmdname === 'node') {
+    var nodePath = path.dirname(process.execPath)
+    var cmds = JSON.parse(env.NYC_CONFIG)._
+    cmds.forEach((cmd) => {
+      var filepath = path.resolve(nodePath, cmd)
+      if (fs.existsSync(filepath + '.cmd')) {
+        var windows = fs.readFileSync(filepath + '.cmd', 'utf8')
+        var unix = fs.readFileSync(filepath, 'utf8')
+        fs.writeFileSync(workingDir + '/' + cmd + '.cmd', windows.replace('"%_prog%"  "%dp0%', '"%_prog%"  "' + nodePath))
+        fs.chmodSync(workingDir + '/' + cmd + '.cmd', '0755')
+        fs.writeFileSync(workingDir + '/' + cmd, unix.replace('"$basedir/node"  "$basedir', '"$basedir/node"  "' + nodePath))
+        fs.chmodSync(workingDir + '/' + cmd, '0755')
+      }
+    })
+  }
   fs.writeFileSync(path.join(workingDir, 'settings.json'), settings)
 
   return workingDir

It’s a very dirty solution honestly but haven’t put much effort.

Please retest using nyc 15 which replaces spawn-wrap with another method of wrapping child processes.

Can confirm @marek629 solution works for me. In my case, I just need to change from

    "test": "mocha -r ts-node/register test/**/*.test.{ts,tsx}",
    "cover": "nyc yarn test",

to

    "cover": "nyc mocha -r ts-node/register test/**/*.test.{ts,tsx}",