sketchthat
sketchthat

Reputation: 2688

npm test differs from manual execution

In my package.json I have the the following;

"scripts": {
  "test": "NODE_ENV=test mocha **/*.spec.js"
}

When I run npm test it only completes one test within my sub directories.

# npm test

sample test
  ✓ single task

1 passing

But when I run the script manually in my console it completes all the tests.

# NODE_ENV=test mocha **/*.spec.js

sample test
  ✓ single task

sample test x2
  ✓ single task x1
  ✓ single task x2
  ✓ single task x3
  ✓ single task x4
  ✓ single task x5

sample test x3
  ✓ single task x1
  ✓ single task x2
  ✓ single task x3
  ✓ single task x4
  ✓ single task x5
  ✓ single task x6

sample test x4
  ✓ single task x1
  ✓ single task x2
  ✓ single task x3
  ✓ single task x4


16 passing (15s)

Why is there a difference when its the same command being run? and how do I get npm test to run all my tests?

Upvotes: 2

Views: 141

Answers (1)

Seth Holladay
Seth Holladay

Reputation: 9539

This is a known issue, caused by the syntax that mocha uses for globbing, which conflicts with the globbing system of the shell itself on many machines. And if you are not careful, the shell will evaluate and expand it first before mocha even has a chance to see it.

The solution is to tell your shell to treat the pattern as a string literal, by wrapping it in quotes. Single quotes are best on *nix systems. If you need Windows support you will have to resort to escaped double quotes, which has some pitfalls on *nix.

NODE_ENV=test mocha '**/*.spec.js'

You will still need to make sure the pattern matches files properly. But using this technique will fix the inconsistent behavior. The difference in behavior is caused by the way npm wraps your test script and runs it.

Ways to help:

Note that other CLI tools that rely upon glob, such as eslint, are also susceptible to this problem and the workaround applies to them as well.

Upvotes: 2

Related Questions