Mocha: Mocha --watch

Created on 15 Aug 2016  路  11Comments  路  Source: mochajs/mocha

When i run the tests with "--watch" flag and change a .jsx file in this path "./src/components/screens/Proposals/New.jsx" the tests aren't reload. But, if i change a test file (tests/unit/components/screens/Proposals/New.js) the tests are reload.

So, i try run pass a regular expression in the flag like "--watch .src/*_/_.jsx" . The console shows only the jsx files in "components/screens" level. So, i think that Mocha is not load correctly the subdirectories.

It is my test command: NODE_PATH=./src mocha --require test/unit/.setup.js --compilers js:babel-register --recursive test/unit

Most helpful comment

Thank you @mmc41. Specifying --watch-extensions made it work:

// in package.json
"scripts": {
  ...
  "watch:test": "mocha -r ts-node/register --watch app/**/*.spec.ts --watch-extensions ts"
}

All 11 comments

are you getting error on $(find api -name '*-test.js')

@govind999 the console does not show errors.

same here

fixed by change .jsx to .js

you'd probably want --compilers jsx:babel-register in addition to --require babel-register (which can replace --compilers js:babel-register), but I can't promise anything.

Mocha's --watch functionality is limited; if you're running into problems, then it's probably being bent in a way which wasn't originally intended. There's no "officially recommended" alternative, but there are a handful of recommendations in the other issues concerning --watch.

thank you very much for your tip @boneskull
this script works just fine for me.

{
  "test": "mocha --compilers js:babel-core/register --compilers jsx:babel-register"
}

There's no "officially recommended" alternative, but there are a handful of recommendations in the other issues concerning --watch.

What are those @boneskull? Are you suggesting something like https://www.npmjs.com/package/watch-run

Discussion in #1780 seems to have converged on Chokidar; there might be others in the items brought up by searching for GitHub issues, without open/closed restriction, that mention "watch" (or "watch module", "watch package" or "watch plugin"?). (There's also some points made there that we really need to fix the problems with the programmatic API to allow external watcher tools to use Mocha as efficiently as the builtin --watch.)

Thank you @ScottFreeCode

There's also some points made there that we really need to fix the problems with the programmatic API to allow external watcher tools to use Mocha as efficiently as the builtin --watch.)

Couldn't agree with that more. I was trying to run test with

watch-run -i -p 'src/**/*' npm test:mocha

Which works just fine, however very slow. Reason? Babel!

"test:mocha": "mocha ./internals/test.setup.js ./src/**/*.spec.js* --require babel-core/register",

Every time file changes mocha requires babel-register which takes time to transpile everything, whereas re-running it with --watch take a bit of initial time to kick in but once up and running it is super fast.

Thank you @boneskull

Just a note : if you run mocha through gulp, here is the notation :

  gulp.src(['src/js/test/**/*.spec.js'])
    .pipe(mocha({
      compilers: ['js:babel-register', 'jsx:babel-register'],
      watch: true,
    }))

Try specifying --watch-extensions to mocha

Thank you @mmc41. Specifying --watch-extensions made it work:

// in package.json
"scripts": {
  ...
  "watch:test": "mocha -r ts-node/register --watch app/**/*.spec.ts --watch-extensions ts"
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

enobufs picture enobufs  路  38Comments

jbnicolai picture jbnicolai  路  37Comments

sagiegurari picture sagiegurari  路  61Comments

teckays picture teckays  路  84Comments

SGD1953 picture SGD1953  路  75Comments