Mocha fails to reload watched files
See original GitHub issueUnder certain circumstances Mocha fails to reload source files in watch mode. Specifically, if a file is watched and modified but only required by a file that is not watched then the modified file is not reloaded and the change is not visible to the test suite.
This issue can be addressed by adding the unwatched files to --watch-files
Steps to Reproduce
Create the following three files
// lib-a/index.js
module.exports = true
// lib-b/index.js
module.exports = require('../lib-a')
// test/main.js
const assert = require('assert')
const value = require('../lib-b')
it('works', function () {
assert(value)
})
Now run
mocha --watch --watch-files lib-a,test
The test suite passes.
Now modify lib-a/index.js
so that the exported value is false
. This will retrigger the tests but they will still succeed.
If lib-b
is add to the --watch-files
list then the modification results in a test failure.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Gulp, Mocha, Watch Not Reloading My Source Files
I had the same issue but i found a fix,. The issue is that require in nodejs is caching your src files when...
Read more >Command-line usage - mocha
In this tutorial we will take a look at the mocha instructions available on the command-line.
Read more >Mocha - the fun, simple, flexible JavaScript test framework
When a test file is loaded, Mocha executes all of its suites and finds–but does not execute–any hooks and tests therein. Top-level hooks,...
Read more >mochajs/mocha - Gitter
And how do I best work around it / the correct way to watch files. Thanks. Juerg B. @juergba ... i am getting...
Read more >CLI Usage · mochapack - GitHub Pages
--bail, -b bail after first test failure --glob only test files matching <pattern> ... mochapack tries to load a webpack-config file named webpack.config.js ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@solaris765 glad it worked out. I’ll keep this bug open because it is indeed an issue albeit not related to your case.
ok, so as I briefly mentioned before, we are using the mongoose package to register models. The code we are currently testing is heavily reliant on those models. And if you’ve ever used mongoose you’ll know that models get registered on to the global mongoose object.
We were using
to get around an issue where the models would throw if they were already defined. (ie. everytime a file changed)
The solution I came up with is to set the global register to undefined before registering
The side effect of my original solution was that changes to the model between test refresh were getting ignored since the model already existed.
Feel free to close this, not a bug in mocha, this was a bug on my end.
Thanks for all the help!