Root hooks not executed when programmatically running Mocha in serial mode #5006
Open
Description
Prerequisites
- Checked that your issue hasn't already been filed by cross-referencing issues with the
faq
label - Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
- 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
- Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.
Description
Hey team!
When running tests programmatically (using new Mocha
), the root hooks provided in require
are not executed if parallel is not set/set to false.
This is a discrepancy with the CLI method of invoking tests that correctly interprets the require
flags and consume them before running the test suites.
Steps to Reproduce
index.js
const Mocha = require("mocha")
const mocha = new Mocha({
require:["./root-hook.js"]
})
mocha.addFile("./test.js")
mocha.run()
test.js
describe("dummy test", () => {
it("works", () => {
console.log("inside the test")
})
})
root-hook.js
exports.mochaHooks = {
beforeAll() {
console.log("before all")
}
}
Expected behavior: Similar behavior as the CLI:
./node_modules/.bin/mocha --require root-hook.js --file test.js
(node:11908) ExperimentalWarning: The ESM module loader is experimental.
before all
dummy test
inside the test
✔ works
1 passing (3ms)
Actual behavior: The root hooks logging "before all" is not being executed
node index.js
dummy test
inside the test
✔ works
1 passing (5ms)
Reproduces how often: 100%
Versions
- The output of
mocha --version
andnode_modules/.bin/mocha --version
: 10.2.0 (reproduced in 9.x as well) - The output of
node --version
: v14.21.3 - Your operating system
- name and version: Amazon Linux 2
- architecture (32 or 64-bit): x86_64
- Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
- Your browser and version (if running browser tests): N/A
- Any third-party Mocha-related modules (and their versions): N/A
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): N/A
Additional Information
I assume the discrepancy comes from lib/cli/run.js
(cli runner) and lib/nodejs/worker.js
(worker thread logic) executing handleRequires
while the bare runner.js
does not.
Activity