☕️ Classic, reliable, trusted test framework for Node.js and the browser
Ensure root level hooks are called when running in watch mode
indieisaconceptedc09bf147be
diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.jsindex a3135c79..0fb214d9 100644--- a/lib/cli/watch-run.js+++ b/lib/cli/watch-run.js@@ -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);@@ -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}) {diff --git a/test/integration/fixtures/options/watch/hook.fixture.js b/test/integration/fixtures/options/watch/hook.fixture.jsnew file mode 100644index 00000000..3463761f--- /dev/null+++ b/test/integration/fixtures/options/watch/hook.fixture.js@@ -0,0 +1,7 @@+module.exports = {+ mochaHooks: {+ ["<hook>"]: function() {+ throw new Error("<hook> Hook Error");+ },+ },+};diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.jsindex 55ea345a..822b28b6 100644--- a/test/integration/options/watch.spec.js+++ b/test/integration/options/watch.spec.js@@ -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'));+ });});});