☕️ Classic, reliable, trusted test framework for Node.js and the browser
fix: surface global setup/teardown errors with a non-zero exit code (#5994)
Mostafa Nazariad81ec68b518
diff --git a/lib/mocha.js b/lib/mocha.jsindex a90f0d86..0de11dca 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -1031,26 +1031,40 @@ Mocha.prototype.run = function (fn) {}};+ const reportFixtureError = (phase, err) => {+ process.stderr.write(+ `\n[mocha] global ${phase} failed:\n${err && err.stack ? err.stack : err}\n`,+ );+ };+const runAsync = async (runner) => {- const context =- this.options.enableGlobalSetup && this.hasGlobalSetupFixtures()- ? await this.runGlobalSetup(runner)- : {};+ let context = {};+ if (this.options.enableGlobalSetup && this.hasGlobalSetupFixtures()) {+ try {+ context = await this.runGlobalSetup(runner);+ } catch (err) {+ reportFixtureError("setup", err);+ return 1;+ }+ }const failureCount = await runner.runAsync({files: this.files,options,});if (this.options.enableGlobalTeardown && this.hasGlobalTeardownFixtures()) {- await this.runGlobalTeardown(runner, { context });+ try {+ await this.runGlobalTeardown(runner, { context });+ } catch (err) {+ reportFixtureError("teardown", err);+ return failureCount + 1;+ }}return failureCount;};- // no "catch" here is intentional. errors coming out of- // Runner#run are considered uncaught/unhandled and caught- // by the `process` event listeners.- // also: returning anything other than `runner` would be a breaking- // change+ // Errors coming out of Runner#run itself remain uncaught/unhandled and are picked up by the `process` event listeners.+ // Only global fixture errors are intercepted above so they surface to the user instead of being silently "swallowed".+ // Also: returning anything other than `runner` would be a breaking change.runAsync(runner).then(done);return runner;diff --git a/test/integration/fixtures/plugins/global-fixtures/global-setup-throws.fixture.js b/test/integration/fixtures/plugins/global-fixtures/global-setup-throws.fixture.jsnew file mode 100644index 00000000..060d94fa--- /dev/null+++ b/test/integration/fixtures/plugins/global-fixtures/global-setup-throws.fixture.js@@ -0,0 +1,9 @@+"use strict";++exports.mochaGlobalSetup = async function () {+ throw new Error("setup kaboom");+};++exports.mochaGlobalTeardown = async function () {+ console.log("teardown should not run");+};diff --git a/test/integration/fixtures/plugins/global-fixtures/global-teardown-throws.fixture.js b/test/integration/fixtures/plugins/global-fixtures/global-teardown-throws.fixture.jsnew file mode 100644index 00000000..48c6ef2a--- /dev/null+++ b/test/integration/fixtures/plugins/global-fixtures/global-teardown-throws.fixture.js@@ -0,0 +1,9 @@+"use strict";++exports.mochaGlobalSetup = async function () {+ console.log("setup ran");+};++exports.mochaGlobalTeardown = async function () {+ throw new Error("teardown kaboom");+};diff --git a/test/integration/plugins/global-fixtures.spec.js b/test/integration/plugins/global-fixtures.spec.jsindex 69bde3de..f17e8ef1 100644--- a/test/integration/plugins/global-fixtures.spec.js+++ b/test/integration/plugins/global-fixtures.spec.js@@ -7,6 +7,7 @@ const {runMochaWatchAsync,copyFixture,DEFAULT_FIXTURE,+ invokeMochaAsync,resolveFixturePath,createTempDir,} = require("../helpers");@@ -80,6 +81,47 @@ describe("global setup/teardown", function () {});});+ describe("when a global fixture throws", function () {+ async function runWithFixture(fixturePath, extraArgs = []) {+ const [, promise] = invokeMochaAsync(+ [+ "--require",+ resolveFixturePath(fixturePath),+ resolveFixturePath(DEFAULT_FIXTURE),+ ...extraArgs,+ ],+ { stdio: "pipe" },+ );+ return promise;+ }++ it("should handle when global teardown throws", async function () {+ const res = await runWithFixture(+ "plugins/global-fixtures/global-teardown-throws",+ );+ expect(+ res,+ "to have failed with output",+ /\[mocha\] global teardown failed[\s\S]*teardown kaboom/,+ );+ expect(res.output, "to match", /setup ran/);+ expect(res.output, "to match", /1 passing/);+ });++ it("should handle when global setup throws", async function () {+ const res = await runWithFixture(+ "plugins/global-fixtures/global-setup-throws",+ );+ expect(+ res,+ "to have failed with output",+ /\[mocha\] global setup failed[\s\S]*setup kaboom/,+ );+ expect(res.output, "not to match", /teardown should not run/);+ expect(res.output, "not to match", /passing/);+ });+ });+describe("when run in watch mode", function () {let tempDir;let testFile;