Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NYC output is empty with mocha installed globally #1029

Closed
Antonius-S opened this issue Mar 14, 2019 · 27 comments
Closed

NYC output is empty with mocha installed globally #1029

Antonius-S opened this issue Mar 14, 2019 · 27 comments
Labels

Comments

@Antonius-S
Copy link

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 |                   |
----------|----------|----------|----------|----------|-------------------|
@coreyfarrell
Copy link
Member

This repo seems to work on Linux. Is this a Windows specific issue? I was able to get coverage by running with globally installed nyc and mocha.

@Antonius-S
Copy link
Author

Probably it's Windows specific. I suspect the reason of this issue is something very dumb, like file paths or similar.

@worc
Copy link

worc commented Mar 21, 2019

output is also empty on mac (node 10.15.3 and nyc ^9.0.1 and yarn 1.13.0, oof yarn/nyc should be updated), it's probably not windows-specific. so, just for the sake of completeness, here's me just brute-forcing my way to a working combination of node, nyc and yarn on mac.

i'm using --all and globbing in a yarn script. so something like yarn run coverage which expands into:

nyc --all mocha --recursive src/**/*.test.js src/**/*_test.js test/

and so far no luck:

node nyc yarn worked?
10.15.3 9.0.1 1.13.0 no ❌
10.15.3 10.3.2 1.15.2 no ❌
10.15.3 11.9.0 1.15.2 no ❌
10.15.3 12.0.2 1.15.2 no ❌
10.15.3 13.3.0 1.15.2 no ❌

@Antonius-S
Copy link
Author

FWIW: I experienced troubles launching mocha tests from VSCode with the above config so I created a workaround, see https://github.com/Antonius-S/Node_Articles/tree/master/Global_nyc_and_mocha

@worc
Copy link

worc commented Mar 22, 2019

another workaround i put together yesterday, which might make people cringe, is to use npm to call the test runners. yarn run coverage expands into nyc npm run test which expands into nyc npm run test-unit && npm run test-integration.

it seems the issue is yarn isn't bubbling up information from the test runs to nyc. but npm can do it.

@coreyfarrell
Copy link
Member

@Antonius-S are you using yarn? If so please see if this issue can be reproduced with npm.

@Antonius-S
Copy link
Author

@coreyfarrell no I use npm

@chankamlam
Copy link

chankamlam commented Mar 28, 2019

屏幕快照 2019-03-29 上午2 24 48

屏幕快照 2019-03-29 上午2 24 33

some problem with me , empty table showing!!!!!
I am using Mac OS with this script => "nyc mocha -t 2000 ./test/index.js"

@chankamlam
Copy link

fixed by #822 (comment)

@worc
Copy link

worc commented Mar 28, 2019

that's not quite a fix, is it? it's more of a workaround to exit mocha cleanly?

@Antonius-S
Copy link
Author

fixed by #822 (comment)

Doesn't help in my case.

@captnseagraves
Copy link

Experiencing this issue as well. Although I have not seen a difference when running locally or globally. I've tried the discussed solutions in other issue threads and have had no luck.

MacOS Mojave@10.14.1,
npm@6.4.1,
mocha@6.1.4,
nyc@14.0.0

@bcoe
Copy link
Member

bcoe commented Apr 30, 2019

@captnseagraves could you create a different PR, with a repo that reproduces your issue? I think that you're probably bumping into something slightly different, this issue specifically relates to a globally installed version of mocha.

@bcoe bcoe added the bug label Apr 30, 2019
@bcoe
Copy link
Member

bcoe commented Apr 30, 2019

we make an effort not to include files when they're outside the directory that is under test, I have a suspicion that this might be the root cause of this bug ... feature? If anyone is interested in taking a look, I would look for our logic that excludes files outside the test directory, and remove it temporarily -- if this does turn out to be the issue, maybe we could add a config option for this.

@Antonius-S
Copy link
Author

we make an effort not to include files when they're outside the directory that is under test, I have a suspicion that this might be the root cause of this bug ... feature?

But files under test are in the same folders in both cases. And global mocha shouldn't generate any files outside package under test.

@marek629
Copy link

Hey, I had the same issue. In my case, following steps helped me:

  1. In package.json file, scripts entries should be written independently, i.e. your coverage script should looks like:
nyc mochapack --reporter nyan app/

instead of

nyc yarn test
  1. Install babel-plugin-istanbul and configure it in .babelrc file

I hope that my solution would helpful for you

@NearHuscarl
Copy link

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}",

@shivashanmugam
Copy link

shivashanmugam commented Jul 23, 2019

Same issue ,
While Running nyc mocha
image

But while Running nyc node_modules/.bin/mocha
image

``

@An-dz
Copy link

An-dz commented 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.

@bcoe
Copy link
Member

bcoe commented Aug 26, 2019

@An-dz thanks for the digging, mind opening even a rough PR with your patch, that way this is less likely to be dropped on the floor.

@An-dz
Copy link

An-dz commented Aug 30, 2019

Done. It's pull request #104 on spawn-wrap.

@coreyfarrell
Copy link
Member

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

@An-dz
Copy link

An-dz commented Mar 7, 2020

Just used nyc 15 with AVA and it's working.

@coreyfarrell
Copy link
Member

If others are still experiencing this issue with nyc 15 please comment with a link to a reproduction and we can reopen.

@RagiRavindran
Copy link

RagiRavindran commented Sep 12, 2020

I am having this same issue. Sorry, I can't post the link to my reproduction.
image
"scripts": {
"test": "node_modules/.bin/mocha --exit",
"test-with-coverage": "nyc --reporter=text --all node_modules/.bin/mocha test/index.test.js",
}
"devDependencies": {
"@babel/preset-env": "^7.11.5",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"mocha": "^8.1.3",
"nodemon": "^2.0.4",
"nyc": "^15.1.0"

@premranderia
Copy link

+1 with same Issue, upgraded from nyc 12.x.x to 15.1.0 and added the .nycrc file and empty coverage

{
  "include": ["dist/app/**/*.js"],
  "exclude": ["dist/app/**/*.spec.js"],
  "reporter": ["html", "cobertura", "text-summary"],
  "all": true,
  "report-dir": "./target/coverage"
}

@RagiRavindran
Copy link

I got coverage after separating app.listen() to another file.

drazisil added a commit to rustymotors/server that referenced this issue Apr 9, 2021
Issule simal to istanbuljs/nyc#1308 (i'm using nvm)

Thanks to istanbuljs/nyc#1029 (comment), mainly
drazisil added a commit to rustymotors/server that referenced this issue Apr 10, 2021
* add service field to tcpmanager logs

* add correct type to logger

* corect service name for logger

* formatting

* corect coverage settings

Issule simal to istanbuljs/nyc#1308 (i'm using nvm)

Thanks to istanbuljs/nyc#1029 (comment), mainly

* remove circular require

* remove final typescript mentions

move typescript to a dev dependency

* exclude the app launchers

* test if deepcode accept this form of jsdoc

* improve jsdocs

* better type management

* remove taprc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.