Skip to content

Commit

Permalink
test_runner: simplify hook running logic
Browse files Browse the repository at this point in the history
This commit removes some asynchronous logic from the runHook()
method and replaces ArrayPrototypeReduce() with a for loop.

PR-URL: nodejs#55963
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
cjihrig authored and Ceres6 committed Nov 26, 2024
1 parent 6ecfe74 commit 01411d9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const {
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeReduce,
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
Expand Down Expand Up @@ -846,13 +845,14 @@ class Test extends AsyncResource {
async runHook(hook, args) {
validateOneOf(hook, 'hook name', kHookNames);
try {
await ArrayPrototypeReduce(this.hooks[hook], async (prev, hook) => {
await prev;
const hooks = this.hooks[hook];
for (let i = 0; i < hooks.length; ++i) {
const hook = hooks[i];
await hook.run(args);
if (hook.error) {
throw hook.error;
}
}, PromiseResolve());
}
} catch (err) {
const error = new ERR_TEST_FAILURE(`failed running ${hook} hook`, kHookFailure);
error.cause = isTestFailureError(err) ? err.cause : err;
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/test-runner/output/abort_hooks.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ not ok 2 - 2 after describe
*
*
*
async Promise.all (index 0)
*
...
# Subtest: test 2
not ok 2 - test 2
Expand All @@ -122,7 +122,7 @@ not ok 2 - 2 after describe
*
*
*
async Promise.all (index 0)
*
...
1..2
not ok 3 - 3 beforeEach describe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ not ok 2 - /test/fixtures/test-runner/output/global_after_should_fail_the_test.j
*
*
*
*
...
1..1
# tests 1
Expand Down
11 changes: 5 additions & 6 deletions test/fixtures/test-runner/output/hooks.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ not ok 3 - before throws
*
*
*
*
...
# Subtest: before throws - no subtests
not ok 4 - before throws - no subtests
Expand All @@ -97,7 +96,6 @@ not ok 4 - before throws - no subtests
*
*
*
*
...
# Subtest: after throws
# Subtest: 1
Expand Down Expand Up @@ -129,6 +127,7 @@ not ok 5 - after throws
*
*
*
*
...
# Subtest: after throws - no subtests
not ok 6 - after throws - no subtests
Expand All @@ -149,6 +148,7 @@ not ok 6 - after throws - no subtests
*
*
*
*
...
# Subtest: beforeEach throws
# Subtest: 1
Expand All @@ -167,9 +167,9 @@ not ok 6 - after throws - no subtests
*
*
*
async Promise.all (index 0)
*
*
new Promise (<anonymous>)
...
# Subtest: 2
not ok 2 - 2
Expand All @@ -188,6 +188,8 @@ not ok 6 - after throws - no subtests
*
*
*
*
async Promise.all (index 0)
...
1..2
not ok 7 - beforeEach throws
Expand Down Expand Up @@ -482,7 +484,6 @@ not ok 15 - t.after throws
*
*
*
*
...
# Subtest: t.after throws - no subtests
not ok 16 - t.after throws - no subtests
Expand All @@ -502,7 +503,6 @@ not ok 16 - t.after throws - no subtests
*
*
*
*
...
# Subtest: t.beforeEach throws
# Subtest: 1
Expand Down Expand Up @@ -765,7 +765,6 @@ not ok 24 - run after when before throws
*
*
*
*
...
# Subtest: test hooks - async
# Subtest: 1
Expand Down
22 changes: 10 additions & 12 deletions test/fixtures/test-runner/output/hooks_spec_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*
*
*
*

before throws - no subtests (*ms)
Error: before
Expand All @@ -38,7 +37,6 @@
*
*
*
*

after throws
1 (*ms)
Expand All @@ -55,6 +53,7 @@
*
*
*
*

after throws - no subtests (*ms)
Error: after
Expand All @@ -67,6 +66,7 @@
*
*
*
*

beforeEach throws
1 (*ms)
Expand All @@ -78,9 +78,9 @@
*
*
*
at async Promise.all (index 0)
*
*
at new Promise (<anonymous>)

2 (*ms)
Error: beforeEach
Expand All @@ -92,6 +92,8 @@
*
*
*
*
at async Promise.all (index 0)

beforeEach throws (*ms)
afterEach throws
Expand Down Expand Up @@ -242,7 +244,6 @@
*
*
*
*

t.after throws - no subtests (*ms)
Error: after
Expand All @@ -255,7 +256,6 @@
*
*
*
*

t.beforeEach throws
1 (*ms)
Expand Down Expand Up @@ -390,7 +390,6 @@
*
*
*
*

test hooks - async
1 (*ms)
Expand Down Expand Up @@ -430,7 +429,6 @@
*
*
*
*

*
before throws - no subtests (*ms)
Expand All @@ -443,7 +441,6 @@
*
*
*
*

*
after throws (*ms)
Expand All @@ -457,6 +454,7 @@
*
*
*
*

*
after throws - no subtests (*ms)
Expand All @@ -470,6 +468,7 @@
*
*
*
*

*
1 (*ms)
Expand All @@ -481,9 +480,9 @@
*
*
*
at async Promise.all (index 0)
*
*
at new Promise (<anonymous>)

*
2 (*ms)
Expand All @@ -496,6 +495,8 @@
*
*
*
*
at async Promise.all (index 0)

*
1 (*ms)
Expand Down Expand Up @@ -633,7 +634,6 @@
*
*
*
*

*
t.after throws - no subtests (*ms)
Expand All @@ -647,7 +647,6 @@
*
*
*
*

*
1 (*ms)
Expand Down Expand Up @@ -776,4 +775,3 @@
*
*
*
*

0 comments on commit 01411d9

Please sign in to comment.