From 5d473c1601f510bc6b300dee197e511355d94129 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Wed, 10 Jun 2020 14:20:20 -0700 Subject: [PATCH] fix --parallel --watch; closes #4327 --- lib/cli/watch-run.js | 28 +++++++++++++++++++++++--- test/integration/options/watch.spec.js | 13 ++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.js index 2c59490a81..a3135c791a 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 259a9416d4..55ea345ab7 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);