Last Updated: February 25, 2016
· mansona

Use npm test, but also npm run-script test

One of the best tips on Coderwall is how you can run your nodejs tests using npm:

npm test

as long as you have the following in your package.json

"scripts": {
  "test": "node test/testrunner.js"

you can see a really nice succinct description here

If you have more than one way to run your tests, generating coverage maps for example, then a lot of people suggest that you write a custom make file.

If you're like me and you like to keep the number of tools/files you're using to a minimum then here is how you can maintain all those test running scripts in one place using the package.json

"scripts": {
  "test": "mocha",
  "test-coverage": "jscoverage lib lib-cov &&  mocha --reporter html-cov > code-coverage.html",
  "anyCrazyCommand": "echo dont mind if i do"

and then you execute them with these commands

npm run-script test
npm run-scirpt test-coverage
npm run-script anyCrazyCommand

As simple as that!

3 Responses
Add your response

I have to say the nodejs system is quite confusing. I stick with using grunt for some building and testing task. And use npm only for package management.

over 1 year ago ·

@nxqd I haven't yet moved over to using grunt for backend stuff, just the frontend. It makes more sense to me there.

Care to share your grunt "test" task?

over 1 year ago ·

@mansona: it's very dependable, because it will depend on your project and how you test your stuffs. And I think you should check grunt first.

over 1 year ago ·