Last Updated: September 09, 2019
·
5.428K
· rstacruz

Auto-run tests in Node.js

Want to run Node.js tests every time a file changes? Add this to your ~/.bash_profile:

alias automocha="supervisor -q -n exit -x mocha -- -b"

And of course, install mocha and supervisor to your global context:

$ npm install -g supervisor mocha

Now just run automocha to invoke mocha every time a file changes.

Bonus: add it to your package.json

Don't want to have these on the global context? That's fair, just merge these definitions into your package.json:

"scripts": {
  "autotest": "./node_modules/.bin/supervisor -q -n exit -x ./node_modules/.bin/mocha -- -b"
},
"devDependencies": {
  "mocha": "~1.10.0",
  "supervisor": "~0.5.2"
}

Then run it:

$ npm run autotest

3 Responses
Add your response

Why not use Karma instead.

over 1 year ago ·

because Karma is for the clientside

over 1 year ago ·

Your package.json could be written as:

"scripts": {
  "autotest": "supervisor -q -n exit -x mocha -- -b"
},
"devDependencies": {
  "mocha": "~1.10.0",
  "supervisor": "~0.5.2"
}

and would behave the same because npm is super clever

over 1 year ago ·