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 #4347 | Ensure root level hooks are called when running in watch mode #4382

Merged
merged 1 commit into from Jul 29, 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
8 changes: 8 additions & 0 deletions lib/cli/watch-run.js
Expand Up @@ -59,6 +59,10 @@ exports.watchParallelRun = (
// in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
newMocha.ui(newMocha.options.ui);

// we need to call `newMocha.rootHooks` to set up rootHooks for the new
// suite
newMocha.rootHooks(newMocha.options.rootHooks);

// in parallel mode, the main Mocha process doesn't actually load the
// files. this flag prevents `mocha.run()` from autoloading.
newMocha.lazyLoadFiles(true);
Expand Down Expand Up @@ -118,6 +122,10 @@ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
// in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
newMocha.ui(newMocha.options.ui);

// we need to call `newMocha.rootHooks` to set up rootHooks for the new
// suite
newMocha.rootHooks(newMocha.options.rootHooks);

return newMocha;
},
afterRun({watcher}) {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/fixtures/options/watch/hook.fixture.js
@@ -0,0 +1,7 @@
module.exports = {
mochaHooks: {
["<hook>"]: function() {
throw new Error("<hook> Hook Error");
},
},
};
37 changes: 37 additions & 0 deletions test/integration/options/watch.spec.js
Expand Up @@ -275,6 +275,43 @@ describe('--watch', function() {
expect(results[1].tests, 'to have length', 2);
});
});

describe('with required hooks', function() {
/**
* Helper for setting up hook tests
*
* @param {string} hookName name of hook to test
* @return {function}
*/
function setupHookTest(hookName) {
return function() {
const testFile = path.join(tempDir, 'test.js');
const hookFile = path.join(tempDir, 'hook.js');

copyFixture('__default__', testFile);
copyFixture('options/watch/hook', hookFile);

replaceFileContents(hookFile, '<hook>', hookName);

return runMochaWatch(
[testFile, '--require', hookFile],
tempDir,
() => {
touchFile(testFile);
}
).then(results => {
expect(results.length, 'to equal', 2);
expect(results[0].failures, 'to have length', 1);
expect(results[1].failures, 'to have length', 1);
});
};
}

it('mochaHooks.beforeAll runs as expected', setupHookTest('beforeAll'));
it('mochaHooks.beforeEach runs as expected', setupHookTest('beforeEach'));
it('mochaHooks.afterAll runs as expected', setupHookTest('afterAll'));
it('mochaHooks.afterEach runs as expected', setupHookTest('afterEach'));
});
});
});

Expand Down