Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix --parallel --watch; closes #4327

Christopher Hiller66ce143efab9
diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js
index 2c59490a..a3135c79 100644
--- a/lib/cli/watch-run.js
+++ b/lib/cli/watch-run.js
@@ -36,11 +36,33 @@ exports.watchParallelRun = (
watchFiles,
watchIgnore,
beforeRun({mocha}) {
- mocha.files = collectFiles(fileCollectParams);
+ // I don't know why we're cloning the root suite.
+ const rootSuite = mocha.suite.clone();
+
+ // this `require` is needed because the require cache has been cleared. the dynamic
+ // exports set via the below call to `mocha.ui()` won't work properly if a
+ // test depends on this module (see `required-tokens.spec.js`).
+ const Mocha = require('../mocha');
+
+ // ... and now that we've gotten a new module, we need to use it again due
+ // to `mocha.ui()` call
+ const newMocha = new Mocha(mocha.options);
+ // don't know why this is needed
+ newMocha.suite = rootSuite;
+ // nor this
+ newMocha.suite.ctx = new Context();
+
+ // reset the list of files
+ newMocha.files = collectFiles(fileCollectParams);
+
+ // because we've swapped out the root suite (see the `run` inner function
+ // in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
+ newMocha.ui(newMocha.options.ui);
+
// in parallel mode, the main Mocha process doesn't actually load the
// files. this flag prevents `mocha.run()` from autoloading.
- mocha.lazyLoadFiles(true);
- return mocha;
+ newMocha.lazyLoadFiles(true);
+ return newMocha;
},
afterRun({watcher}) {
blastCache(watcher);
diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.js
index 259a9416..55ea345a 100644
--- a/test/integration/options/watch.spec.js
+++ b/test/integration/options/watch.spec.js
@@ -31,6 +31,19 @@ describe('--watch', function() {
});
});
+ describe('when in parallel mode', function() {
+ it('reruns test when watched test file is touched', function() {
+ const testFile = path.join(tempDir, 'test.js');
+ copyFixture('__default__', testFile);
+
+ return runMochaWatch(['--parallel', testFile], tempDir, () => {
+ touchFile(testFile);
+ }).then(results => {
+ expect(results, 'to have length', 2);
+ });
+ });
+ });
+
it('reruns test when file matching --watch-files changes', function() {
const testFile = path.join(tempDir, 'test.js');
copyFixture('__default__', testFile);