NodeJS - Setting up watch to auto execute test cases


In this article, Iā€™m going to illustrate how to auto-execute test cases with scripts of package.json

Please view part-1 of setting up mocha for testing

{
  "name": "node-web-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha **/*.test.js",
    "test-watch": "nodemon --exec 'npm test'" // this will run `npm test` as soon as code is changed
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",
    "hbs": "^4.0.1"
  },
  "devDependencies": {
    "mocha": "^4.0.1"
  }
}

Now go back to terminal & run following command to start auto-execution of test cases.

npm run test-watch

Above should give you the output as follows.

node-web-server$ npm run test-watch

> node-web-server@1.0.0 test-watch /Users/sagar/LearnNodeJS/node-web-server
> nodemon --exec 'npm test'

[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm test`

> node-web-server@1.0.0 test /Users/sagar/LearnNodeJS/node-web-server
> mocha **/*.test.js



  āœ“ should add two numbers
  āœ“ should square a number

  2 passing (6ms)

[nodemon] clean exit - waiting for changes before restart
^C
node-web-server$