Skip to content

Commit edc09bf

Browse files
indieisaconceptboneskull
authored andcommitted
Ensure root level hooks are called when running in watch mode
## Rationale Tests with root level hooks are ignored when running in watch mode. This occurs on initial & subsequent runs. ## Changes - updated `lib/cli/run-watch` to re-initialise rootHooks As we have swapped out the root suite it is necessary to re-initialize root level hooks again. - Extended integration tests for watch to take into consideration hooks ## Refs - /issues/4347
1 parent 29012aa commit edc09bf

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

lib/cli/watch-run.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ exports.watchParallelRun = (
5959
// in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
6060
newMocha.ui(newMocha.options.ui);
6161

62+
// we need to call `newMocha.rootHooks` to set up rootHooks for the new
63+
// suite
64+
newMocha.rootHooks(newMocha.options.rootHooks);
65+
6266
// in parallel mode, the main Mocha process doesn't actually load the
6367
// files. this flag prevents `mocha.run()` from autoloading.
6468
newMocha.lazyLoadFiles(true);
@@ -118,6 +122,10 @@ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
118122
// in `createRerunner`), we need to call `mocha.ui()` again to set up the context/globals.
119123
newMocha.ui(newMocha.options.ui);
120124

125+
// we need to call `newMocha.rootHooks` to set up rootHooks for the new
126+
// suite
127+
newMocha.rootHooks(newMocha.options.rootHooks);
128+
121129
return newMocha;
122130
},
123131
afterRun({watcher}) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
mochaHooks: {
3+
["<hook>"]: function() {
4+
throw new Error("<hook> Hook Error");
5+
},
6+
},
7+
};

test/integration/options/watch.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,43 @@ describe('--watch', function() {
275275
expect(results[1].tests, 'to have length', 2);
276276
});
277277
});
278+
279+
describe('with required hooks', function() {
280+
/**
281+
* Helper for setting up hook tests
282+
*
283+
* @param {string} hookName name of hook to test
284+
* @return {function}
285+
*/
286+
function setupHookTest(hookName) {
287+
return function() {
288+
const testFile = path.join(tempDir, 'test.js');
289+
const hookFile = path.join(tempDir, 'hook.js');
290+
291+
copyFixture('__default__', testFile);
292+
copyFixture('options/watch/hook', hookFile);
293+
294+
replaceFileContents(hookFile, '<hook>', hookName);
295+
296+
return runMochaWatch(
297+
[testFile, '--require', hookFile],
298+
tempDir,
299+
() => {
300+
touchFile(testFile);
301+
}
302+
).then(results => {
303+
expect(results.length, 'to equal', 2);
304+
expect(results[0].failures, 'to have length', 1);
305+
expect(results[1].failures, 'to have length', 1);
306+
});
307+
};
308+
}
309+
310+
it('mochaHooks.beforeAll runs as expected', setupHookTest('beforeAll'));
311+
it('mochaHooks.beforeEach runs as expected', setupHookTest('beforeEach'));
312+
it('mochaHooks.afterAll runs as expected', setupHookTest('afterAll'));
313+
it('mochaHooks.afterEach runs as expected', setupHookTest('afterEach'));
314+
});
278315
});
279316
});
280317

0 commit comments

Comments
 (0)