From 789b7eebff58d404ac8f060376a75e26b514f9af Mon Sep 17 00:00:00 2001 From: Jithil Date: Thu, 27 Oct 2022 20:26:49 +1100 Subject: [PATCH] test_runner: fix afterEach not running on test failures test_runner: fix afterEach not running on test failures --- lib/internal/test_runner/test.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index c7cf9e36129d96..85ee191d81007f 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -481,8 +481,9 @@ class Test extends AsyncResource { return; } + const { args, ctx } = this.getRunArgs(); + try { - const { args, ctx } = this.getRunArgs(); if (this.parent?.hooks.beforeEach.length > 0) { await this.parent[kRunHook]('beforeEach', { args, ctx }); } @@ -517,10 +518,6 @@ class Test extends AsyncResource { return; } - if (this.parent?.hooks.afterEach.length > 0) { - await this.parent[kRunHook]('afterEach', { args, ctx }); - } - this.pass(); } catch (err) { if (isTestFailureError(err)) { @@ -532,6 +529,12 @@ class Test extends AsyncResource { } else { this.fail(new ERR_TEST_FAILURE(err, kTestCodeFailure)); } + } finally { + const afterEach = this.parent?.hooks?.afterEach || []; + const shouldRunAfterEach = afterEach.length > 0; + if (shouldRunAfterEach) { + await this.parent[kRunHook]('afterEach', { args, ctx }); + } } // Clean up the test. Then, try to report the results and execute any