Skip to content

Commit 66ce143

Browse files
committed
fix --parallel --watch; closes #4327
1 parent e0e6568 commit 66ce143

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/cli/watch-run.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,33 @@ exports.watchParallelRun = (
3636
watchFiles,
3737
watchIgnore,
3838
beforeRun({mocha}) {
39-
mocha.files = collectFiles(fileCollectParams);
39+
// I don't know why we're cloning the root suite.
40+
const rootSuite = mocha.suite.clone();
41+
42+
// this `require` is needed because the require cache has been cleared. the dynamic
43+
// exports set via the below call to `mocha.ui()` won't work properly if a
44+
// test depends on this module (see `required-tokens.spec.js`).
45+
const Mocha = require('../mocha');
46+
47+
// ... and now that we've gotten a new module, we need to use it again due
48+
// to `mocha.ui()` call
49+
const newMocha = new Mocha(mocha.options);
50+
// don't know why this is needed
51+
newMocha.suite = rootSuite;
52+
// nor this
53+
newMocha.suite.ctx = new Context();
54+
55+
// reset the list of files
56+
newMocha.files = collectFiles(fileCollectParams);
57+
58+
// because we've swapped out the root suite (see the `run` inner function
59+
// in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
60+
newMocha.ui(newMocha.options.ui);
61+
4062
// in parallel mode, the main Mocha process doesn't actually load the
4163
// files. this flag prevents `mocha.run()` from autoloading.
42-
mocha.lazyLoadFiles(true);
43-
return mocha;
64+
newMocha.lazyLoadFiles(true);
65+
return newMocha;
4466
},
4567
afterRun({watcher}) {
4668
blastCache(watcher);

test/integration/options/watch.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ describe('--watch', function() {
3131
});
3232
});
3333

34+
describe('when in parallel mode', function() {
35+
it('reruns test when watched test file is touched', function() {
36+
const testFile = path.join(tempDir, 'test.js');
37+
copyFixture('__default__', testFile);
38+
39+
return runMochaWatch(['--parallel', testFile], tempDir, () => {
40+
touchFile(testFile);
41+
}).then(results => {
42+
expect(results, 'to have length', 2);
43+
});
44+
});
45+
});
46+
3447
it('reruns test when file matching --watch-files changes', function() {
3548
const testFile = path.join(tempDir, 'test.js');
3649
copyFixture('__default__', testFile);

0 commit comments

Comments
 (0)