question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NYC output is empty with mocha installed globally

See original GitHub issue

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 |                   |
----------|----------|----------|----------|----------|-------------------|

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
shivashanmugamcommented, Jul 23, 2019

Same issue , While Running nyc mocha
image

But while Running nyc node_modules/.bin/mocha image

``

2reactions
An-dzcommented, Aug 25, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - No coverage nyc mocha - Stack Overflow
I have installed mocha globally and when nyc referring mocha globally it does't show anything in the coverage report.
Read more >
mochajs/mocha - Gitter
Hey, I'm using JSDOM to build up an empty window for my mocha tests, and I'm running into a JSDOM error for which...
Read more >
Mocha documentation — DevDocs
Installation. Install with npm globally: $ npm install --global mocha. or as a development dependency for your project: $ npm install --save-dev mocha....
Read more >
Unit testing in Node.js - IBM Developer
Next, you'll install each of the test packages. Install Mocha. From the terminal window, enter this command: npm i --save-dev mocha. The output...
Read more >
Nyc And Mocha Code Coverage Failing When Running In ...
With Istanbul installed, prepend your existing Mocha command with the NYC binary. ... NYC output is empty · Issue #822 · istanbuljs/nyc ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found