fdcsyq
Last Updated: February 25, 2016
·
3.641K
· rstacruz
F8ec7f90daf8b1defb8e318d663c0f17

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
Say Thanks
Respond

3 Responses
Add your response

6840
Me

Why not use Karma instead.

over 1 year ago ·
6932
82997337aa826114cffe0f9af85928a0

because Karma is for the clientside

over 1 year ago ·
6978
Eb3e104452d654350a5d1a65caa2e49e

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 ·