Skip to content

Commit

Permalink
test_runner: fix afterEach not running on test failures
Browse files Browse the repository at this point in the history
test_runner: fix afterEach not running on test failures
  • Loading branch information
MrJithil committed Oct 27, 2022
1 parent a69a300 commit 789b7ee
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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
Expand Down

0 comments on commit 789b7ee

Please sign in to comment.