3

I'm performing mocha tests on my React components that I wrote using ES6 syntax. I'm using Istanbul to do code coverage tests. When i set my NODE_ENV to 'test`, I get the following output:

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|

As can be seen, it says unknown for everything. When I remove the NODE_ENV variable, it works fine. What can I do to run this test in the above environment?

morghulis
  • 145
  • 11

2 Answers2

3

I have been trying the last two days to set up a similar environment using babel, nyc and react and faced similar problems. How are you setting the NODE_ENV? Are you on Windows?

This is what I had in my package.json:

"test:c": "set NODE_ENV=test && npm run coverage"

When this is executed, windows actually sets the NODE_ENV as "test " not as "test". Notice the trailing whitespace.

I fixed this by removing the space from the script :

"test:c": "set NODE_ENV=test&& npm run coverage"
Marcus
  • 433
  • 1
  • 4
  • 12
  • I used the npm package called `cross-env`. So my test script is `"test": "cross-env NODE_ENV=test nyc mocha"` And yes, I'm on Windows. – morghulis Jun 08 '17 at 14:24
  • well it seems like you could have the same issue, in your case NODE_ENV could be interpreted as 'test ', with a whitespace Try splitting the script in two scripts like this: `"test" : "cross-env NODE_ENV=test&&npm run actual_test", "actual_test" : "nyc mocha"` That way NODE_ENV should be set correctly – Marcus Jun 08 '17 at 15:10
0

Use cross-env (https://www.npmjs.com/package/cross-env)

In package.json:

"test": "cross-env NODE_ENV=test nyc mocha --exit && npm run coverage"

DougA
  • 1,213
  • 9
  • 17