goodvibration
goodvibration

Reputation: 6206

Testing via npm command line

I have two types of test suites - normal and coverage.

At present, I am allowing one of them via npm test and one of them via npm start:

  "scripts": {
    "test": "node scripts/run-truffle-tests.js",
    "start": "node scripts/run-sol-coverage.js"
  }

I have a feeling that npm start was not originally designated for this purpose.

Is there a better way to implement this?

I was thinking of passing an argument to npm test, but I'm not sure that npm would pass it on to the script which it is set to invoke.

Upvotes: 0

Views: 5238

Answers (1)

zero298
zero298

Reputation: 26867

Add more scripts.

I usually have the tests for actual, full, single run unit tests to work with CI and other scripts for variations:

{
  "scripts": {
    "test": "node scripts/run-truffle-tests.js && npm run test:coverage",
    "test:continuous": "karma start some.config --run-continuous",
    "test:coverage": "node scripts/run-sol-coverage.js"
    "start": "node index.js"
  }
}

You can also chain commands with && which will cause the script to be run in sequence and the "total" error code will propagate. In other words, using the test I have above, will run both the unit test and the coverage test. If either of them return a non-zero exit code, npm will consider the whole test process to have failed.


Bear in mind that for custom scripts not named exactly start and test as well as the other designated scripts found in the docs here: npm#scripts, must be run with

npm run scriptname

instead of just

npm scriptname

So in my above example, you would test coverage with:

npm run test:coverage

Also, the : is just a convention. As far as I know it's not special.


Additionally, you can

pass [argument] on to the script which it is set to invoke

Whenever use use npm test, what is basically happening is that npm runs whatever String value is set in the package.json's scripts.test as a process, as though you had typed that String into the shell yourself. It then looks at the return code and if it's 0, it reports as everything being ok; if it's non-zero, it prints an error.

Upvotes: 3

Related Questions