Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix: fail nested tests on beforeEach failure (#6292)

MaheshDesua75b43b0d69f
diff --git a/lib/runner.js b/lib/runner.js
index a63cc041..4e852ac9 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -835,22 +835,12 @@ Runner.prototype.runTests = function (suite, fn) {
!after &&
self._failedBeforeEachHook
) {
- // Fail all remaining tests in the suite
- var remainingTests = tests.slice();
- remainingTests.forEach(function (t) {
- if (!t.state) {
- var testError = createHookSkipError(
- self._failedBeforeEachHook.title,
- self._failedBeforeEachHook.error,
- );
-
- t.state = STATE_FAILED;
- self.failures++;
- self.emit(constants.EVENT_TEST_BEGIN, t);
- self.emit(constants.EVENT_TEST_FAIL, t, testError);
- self.emit(constants.EVENT_TEST_END, t);
- }
- });
+ // Fail all remaining tests in the suite and its nested suites
+ self.failAffectedTests(
+ suite,
+ self._failedBeforeEachHook.error,
+ self._failedBeforeEachHook.title,
+ );
// Clear the stored hook info
delete self._failedBeforeEachHook;
}
diff --git a/test/integration/fixtures/hooks/before-each-hook-error-with-fail-affected-nested.fixture.js b/test/integration/fixtures/hooks/before-each-hook-error-with-fail-affected-nested.fixture.js
new file mode 100644
index 00000000..0aca390d
--- /dev/null
+++ b/test/integration/fixtures/hooks/before-each-hook-error-with-fail-affected-nested.fixture.js
@@ -0,0 +1,18 @@
+'use strict';
+
+describe('outer', function () {
+ beforeEach(function () {
+ throw new Error('error in `beforeEach` hook');
+ });
+ it('direct test', function () {
+ // This should be reported as failed due to beforeEach hook failure
+ });
+ describe('nested', function () {
+ it('nested test 1', function () {
+ // This should be reported as failed due to beforeEach hook failure
+ });
+ it('nested test 2', function () {
+ // This should be reported as failed due to beforeEach hook failure
+ });
+ });
+});
diff --git a/test/integration/hook-err.spec.cjs b/test/integration/hook-err.spec.cjs
index 96d0538a..161aa309 100644
--- a/test/integration/hook-err.spec.cjs
+++ b/test/integration/hook-err.spec.cjs
@@ -332,6 +332,31 @@ describe("hook error handling", function () {
});
});
+ describe("error in `beforeEach` hook with nested suites", function () {
+ it("should fail tests in nested suites", function (done) {
+ runMochaJSON(
+ "hooks/before-each-hook-error-with-fail-affected-nested.fixture.js",
+ ["--fail-hook-affected-tests"],
+ (err, res) => {
+ if (err) {
+ return done(err);
+ }
+ expect(res, "to have failed")
+ .and("to have failed test count", 4)
+ .and(
+ "to have failed test",
+ '"before each" hook for "direct test"',
+ )
+ .and("to have failed test", "direct test")
+ .and("to have failed test", "nested test 1")
+ .and("to have failed test", "nested test 2")
+ .and("to have passed test count", 0);
+ done();
+ },
+ );
+ });
+ });
+
describe("non-Error thrown in `before` hook", function () {
it("should handle null, undefined, and other non-Error values", function (done) {
runMochaJSON(
diff --git a/test/unit/runner.spec.cjs b/test/unit/runner.spec.cjs
index 2edf99bb..0a8fd056 100644
--- a/test/unit/runner.spec.cjs
+++ b/test/unit/runner.spec.cjs
@@ -817,6 +817,69 @@ describe("Runner", function () {
});
});
+ describe("runTests()", function () {
+ describe("when failHookAffectedTests is enabled", function () {
+ it("should fail remaining and nested tests on a failed `beforeEach` hook", function (done) {
+ var hookError = new Error("failed hook");
+ suite.beforeEach(function () {
+ throw hookError;
+ });
+ var nested = new Suite("nested");
+ suite.addSuite(nested);
+ var directTest = new Test("direct test", noop);
+ suite.addTest(directTest);
+ var nestedTest = new Test("nested test", noop);
+ nested.addTest(nestedTest);
+
+ var newRunner = new Runner(suite, { failHookAffectedTests: true });
+ var failedTests = [];
+ newRunner.on(EVENT_TEST_FAIL, function (test) {
+ if (test.type === "test") {
+ failedTests.push(test.title);
+ }
+ });
+
+ newRunner.runTests(suite, function () {
+ expect(newRunner.failures, "to be", 3);
+ expect(directTest.state, "to be", STATE_FAILED);
+ expect(nestedTest.state, "to be", STATE_FAILED);
+ expect(failedTests, "to have length", 2);
+ expect(failedTests, "to contain", "direct test", "nested test");
+ done();
+ });
+ });
+ });
+
+ describe("when failHookAffectedTests is disabled", function () {
+ it("should not fail nested tests on a failed `beforeEach` hook", function (done) {
+ var hookError = new Error("failed hook");
+ suite.beforeEach(function () {
+ throw hookError;
+ });
+ var nested = new Suite("nested");
+ suite.addSuite(nested);
+ var directTest = new Test("direct test", noop);
+ suite.addTest(directTest);
+ var nestedTest = new Test("nested test", noop);
+ nested.addTest(nestedTest);
+
+ var failedTests = [];
+ runner.on(EVENT_TEST_FAIL, function (test) {
+ if (test.type === "test") {
+ failedTests.push(test.title);
+ }
+ });
+
+ runner.runTests(suite, function () {
+ expect(runner.failures, "to be", 1);
+ expect(directTest.state, "to be undefined");
+ expect(nestedTest.state, "to be undefined");
+ done();
+ });
+ });
+ });
+ });
+
describe("allowUncaught()", function () {
it("should allow unhandled errors to propagate through", function () {
var newRunner = new Runner(suite);