Skip to content

Commit

Permalink
fix --parallel --watch; closes #4327
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jun 10, 2020
1 parent e0e6568 commit 5d473c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/cli/watch-run.js
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/integration/options/watch.spec.js
Expand Up @@ -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);
Expand Down

0 comments on commit 5d473c1

Please sign in to comment.