Skip to content

Commit

Permalink
Ensure root level hooks are called when running in watch mode
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
indieisaconcept authored and boneskull committed Jul 29, 2020
1 parent 29012aa commit edc09bf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cli/watch-run.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit edc09bf

Please sign in to comment.