Istanbul: Cannot get Istanbul to run with Mocha

Created on 5 Apr 2016  ยท  6Comments  ยท  Source: gotwarlost/istanbul

Hello,

I am using this starter kit and cannot get Istanbul to run with Mocha. I have run the following command:

npm install istanbul

And added this to my package.json:

"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000"

As I saw suggested in one of the comments here. However when I run npm run coverage I get the following error:

No coverage information was collected, exit without writing coverage information
/Users/my/path/node_modules/mocha/lib/utils.js:628
        throw new Error("cannot resolve path (or pattern) '" + path + "'");
        ^

Error: cannot resolve path (or pattern) 'test'
    at Object.lookupFiles (/Users/my/path/node_modules/mocha/lib/utils.js:628:15)
    at /Users/my/path/node_modules/mocha/bin/_mocha:326:30
    at Array.forEach (native)
    at Object.<anonymous> (/Users/my/path/node_modules/mocha/bin/_mocha:325:6)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Object.Module._extensions.(anonymous function) [as .js] /Users/my/path/node_modules/istanbul/lib/hook.js:109:37)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at runFn (/Users/my/path/node_modules/istanbul/lib/command/common/run-with-cover.js:122:16)
    at /Users/my/path/node_modules/istanbul/lib/command/common/run-with-cover.js:251:17
    at /Users/my/path/node_modules/istanbul/lib/util/file-matcher.js:68:16
    at /Users/my/path/node_modules/istanbul/node_modules/async/lib/async.js:52:16
    at /Users/my/path/node_modules/istanbul/node_modules/async/lib/async.js:361:13
    at /Users/my/path/node_modules/istanbul/node_modules/async/lib/async.js:52:16
    at done (/Users/my/path/node_modules/istanbul/node_modules/async/lib/async.js:246:17)
    at /Users/my/path/node_modules/istanbul/node_modules/async/lib/async.js:44:16
    at /Users/my/path/node_modules/istanbul/node_modules/async/lib/async.js:358:17
    at LOOP (fs.js:1613:14)
    at nextTickCallbackWith0Args (node.js:452:9)
    at process._tickCallback (node.js:381:13)

Any ideas why I might be getting this? Sorry, I'm very new to Mocha and Istanbul.

Most helpful comment

I believe mocha by default looks for tests in a test/ directory or test.js file at the root of your project (typically the same directory as package.json).

The error you are seeing is likely because mocha cannot find a test/ directory or test.js file — hence cannot resolve path... 'test'. So when istanbul runs mocha, mocha can't find any tests to run.

Notice that the npm script running mocha in the starter kit is as follows (under scripts in package.json):

"test-node": "./node_modules/mocha/bin/mocha $(find api -name '*-test.js') --compilers js:babel-core/register",

It seems the starter kit you are using has elected to not use a test/ directory at all, and instead includes test files right next to components with a -test suffix. Those files are picked up by the find api -name '*-test.js' part of the script and passed to mocha. Some people prefer this method for testing JS in the React ecosystem.

Compare the following hypothetical file trees:

.
โ”œโ”€โ”€ src
โ”‚ย ย  โ””โ”€โ”€ components
โ”‚ย ย      โ””โ”€โ”€ Foo.js
โ””โ”€โ”€ test
    โ””โ”€โ”€ components
        โ””โ”€โ”€ Foo.js
.
โ””โ”€โ”€ src
    โ””โ”€โ”€ components
        โ”œโ”€โ”€ Foo.js
        โ””โ”€โ”€ Foo-test.js

By default, mocha expects tests to be structured something like the first tree. Some people prefer the second structure, and mocha gives you the ability to explicitly declare where your test files are to accommodate this.

All that to say, either put your tests in a test/ dir (and remember to change the test-node script in package.json to reflect this), or explicitly tell mocha where your tests are in your coverage script. If you like the way the starter kit is doing it, I'd try using the same find command from test-node (and the --compilers flag as well to support ES6):

"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000 --compilers js:babel-core/register $(find api -name '*-test.js')"

As a side note, I believe $(find api -name '*-test.js') will pick up files in a test/ dir as well, so hypothetically you could have something like this:

.
โ”œโ”€โ”€ src
โ”‚ย ย  โ””โ”€โ”€ components
โ”‚ย ย      โ””โ”€โ”€ Foo.js
โ””โ”€โ”€ test
    โ””โ”€โ”€ components
        โ””โ”€โ”€ Foo-test.js

That seems silly to me, though, because then you could just use the --recursive flag and not find.

Also note that you will probably continue to get the same error until you write your first test.

Hope that helps, and good luck!

All 6 comments

I believe mocha by default looks for tests in a test/ directory or test.js file at the root of your project (typically the same directory as package.json).

The error you are seeing is likely because mocha cannot find a test/ directory or test.js file — hence cannot resolve path... 'test'. So when istanbul runs mocha, mocha can't find any tests to run.

Notice that the npm script running mocha in the starter kit is as follows (under scripts in package.json):

"test-node": "./node_modules/mocha/bin/mocha $(find api -name '*-test.js') --compilers js:babel-core/register",

It seems the starter kit you are using has elected to not use a test/ directory at all, and instead includes test files right next to components with a -test suffix. Those files are picked up by the find api -name '*-test.js' part of the script and passed to mocha. Some people prefer this method for testing JS in the React ecosystem.

Compare the following hypothetical file trees:

.
โ”œโ”€โ”€ src
โ”‚ย ย  โ””โ”€โ”€ components
โ”‚ย ย      โ””โ”€โ”€ Foo.js
โ””โ”€โ”€ test
    โ””โ”€โ”€ components
        โ””โ”€โ”€ Foo.js
.
โ””โ”€โ”€ src
    โ””โ”€โ”€ components
        โ”œโ”€โ”€ Foo.js
        โ””โ”€โ”€ Foo-test.js

By default, mocha expects tests to be structured something like the first tree. Some people prefer the second structure, and mocha gives you the ability to explicitly declare where your test files are to accommodate this.

All that to say, either put your tests in a test/ dir (and remember to change the test-node script in package.json to reflect this), or explicitly tell mocha where your tests are in your coverage script. If you like the way the starter kit is doing it, I'd try using the same find command from test-node (and the --compilers flag as well to support ES6):

"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000 --compilers js:babel-core/register $(find api -name '*-test.js')"

As a side note, I believe $(find api -name '*-test.js') will pick up files in a test/ dir as well, so hypothetically you could have something like this:

.
โ”œโ”€โ”€ src
โ”‚ย ย  โ””โ”€โ”€ components
โ”‚ย ย      โ””โ”€โ”€ Foo.js
โ””โ”€โ”€ test
    โ””โ”€โ”€ components
        โ””โ”€โ”€ Foo-test.js

That seems silly to me, though, because then you could just use the --recursive flag and not find.

Also note that you will probably continue to get the same error until you write your first test.

Hope that helps, and good luck!

@codekirei - thanks for contributing the detailed explanation!

@codekirei @gotwarlost Thanks for the responses, they helped me understand what's going on better. However I am not getting the report. I changed the line in my package.json to:

"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000 --compilers js:babel-register $(find api -name '*-test.js')"

Now when I call npm run coverage I get the following:

> [email protected] coverage /path/to/myapp
> ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec -t 5000 --compilers js:babel-register $(find api -name '*-test.js')

==>     ERROR: No PORT environment variable has been specified


  test one
    โœ“ completes thing 1
    โœ“ completes thing 2
    ...
    โœ“ completes thing 49

  49 passing (8s)

No coverage information was collected, exit without writing coverage information

As you can see I'm not getting coverage information. Why would this be?

Progress! :grin:

The PORT error is coming from here, but I believe that's unrelated to your (lack of) coverage issues.

Hmmm. Perhaps try the 1.0 branch of istanbul, I think it works better with ES6. You are writing your tests in ES6, yes? If not, you shouldn't be using --compilers js:babel-register. Apologies if I led you astray there.

npm install --save-dev [email protected]

"coverage": "istanbul cover _mocha -- --ui bdd -R spec -t 5000 --compilers js:babel-register $(find api -name '*-test.js')" 

@codekirei Thank you! That worked like a charm! :+1:

Great! Happy coding :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amoufaddel picture amoufaddel  ยท  28Comments

davglass picture davglass  ยท  49Comments

dcrockwell picture dcrockwell  ยท  37Comments

ottes picture ottes  ยท  36Comments

schulzch picture schulzch  ยท  151Comments