Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix --parallel --watch; closes #4327 #4328

Merged
merged 1 commit into from Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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