10

I'm testing out a simple application (from the Mocha tutorial code here https://marcofranssen.nl/using-mocha-chai-sinon-to-test-node-js/) to try to get Istanbul to work. My problem is that Istanbul works fine to give me a coverage summary, but then spits out an error for some reason and I'm not sure why. My tests all pass so they're hopefully not the problem. Here is how I run Istanbul:

$ istanbul cover test.js

=============================================================================
Writing coverage object [C:\Users\path\test\coverage\coverage.json]
Writing coverage reports at [C:\Users\path\test\coverage]
=============================================================================

=============================== Coverage summary ===============================

Statements   : 54.55% ( 6/11 )
Branches     : 100% ( 0/0 )
Functions    : 0% ( 0/2 )
Lines        : 54.55% ( 6/11 )
================================================================================
ReferenceError: describe is not defined
    at Object.<anonymous> (C:\Users\path\test.js:9:386)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions.(anonymous function) [as .js] (C:\Users\path
\AppData\Roaming\npm\node_modules\istanbul\lib\hook.js:107:24)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at runFn (C:\Users\path\AppData\Roaming\npm\node_modules\istanbul\lib\com
mand\common\run-with-cover.js:122:16)
    at C:\Users\path\AppData\Roaming\npm\node_modules\istanbul\lib\command\co
mmon\run-with-cover.js:251:17
    at C:\Users\path\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-
matcher.js:68:16
    at C:\Users\path\AppData\Roaming\npm\node_modules\istanbul\node_modules\a
sync\lib\async.js:52:16

I'm not too sure why describe is not recognized as all the tests run fine and pass.

user3180077
  • 633
  • 1
  • 8
  • 16

1 Answers1

10

Found the problem: mocha wasn't installed globally so I had to reference it:

istanbul cover /path/to/bin/_mocha path/to/test.js
user3180077
  • 633
  • 1
  • 8
  • 16
  • 3
    **note that underscore!** It's `_mocha` and not `mocha`. This will also work if you do have Mocha globally, i.e. `istanbul cover _mocha`. For more info on 'why that underscore', read [this issue](https://github.com/gotwarlost/istanbul/issues/44) – sgtdck Mar 09 '16 at 09:56
  • 1
    Ha! I had the same issue despite having local and global installs of both libs. After a number of searches I kept landing back in here and finally arrived at @sgtdck 's comment > https://github.com/gotwarlost/istanbul/issues/44 where ```istanbul cover _mocha -- -u exports -R spec``` fixed the problem without any other changes. Now ```npm test``` runs mocha and istanbul just fine! Thanks peeps! – datafunk Oct 07 '16 at 22:23