question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocha fails to reload watched files

See original GitHub issue

Under 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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
geigerzaehlercommented, Sep 14, 2019

@solaris765 glad it worked out. I’ll keep this bug open because it is indeed an issue albeit not related to your case.

0reactions
solaris765commented, Sep 13, 2019

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

module.exports =
    mongoose.models.organizational_units ||
    mongoose.model(`organizational_units`, schema)

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

mongoose.models.roles = undefined
module.exports = mongoose.model(`roles`, schema)

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found