Mr Mikkél
Mr Mikkél

Reputation: 2643

npm + Mocha --watch (not accurately watching files)

I'm using Mocha with npm, and doing an "npm test" to start up mocha. It has the --watch enabled in the mocha.opts, but for some reason it's not accurately watching. When I make a change to my test file, and then save it, whether I put in a faulty test or an ok test, I get the same thing: 0 passing (in green), whereas, when I first run mocha, I get 2 passing.

Is there something I am missing?

Thanks!

Upvotes: 22

Views: 11380

Answers (2)

Sam Mikes
Sam Mikes

Reputation: 10828

I am able to get this to work. I wonder if the problem is that your mocha.opts needs to be in the test subdirectory?

In any case, a working proof of concept is on npm as smikes-mocha-watch-example, and you can test this via

mkdir test
npm install smikes-mocha-watch-example
cd node_modules/smikes-mocha-watch-example
npm install
npm test

I will suggest that you probably don't want npm test to run mocha --watch since npm test is sometimes run programmatically, and it would look like an indefinite hang if it ran with --watch. A better solution might be to use mocha as the test script and mocha --watch as a new script, e.g., "watch":

$ cat package.json
{
  "scripts": {
    "test": "mocha",
    "watch": "mocha --watch"
   }
}
$ npm test
... runs mocha
$ npm run watch
... runs mocha --watch

Upvotes: -2

Toby Hede
Toby Hede

Reputation: 37133

You need to ensure that npm passes the CLI opts through to mocha using:

npm test -- --watch

This will pass the watch flag when npm runs mocha.

Upvotes: 27

Related Questions