Skip to content

Commit 3ae2015

Browse files
committed
test_runner: fix ordering of test hooks
For tests with subtests the before hook was being run after the beforeEach hook, which is the opposite to test suites and expectations. Also, a function was being used to close over the after hooks, but at the point it was being run the after hooks were not yet set up. Fixes #47915
1 parent 0b3fcfc commit 3ae2015

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/internal/test_runner/test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ class Test extends AsyncResource {
477477
this.parent.addPendingSubtest(deferred);
478478
return deferred.promise;
479479
}
480-
481480
return this.run();
482481
}
483482

@@ -525,24 +524,20 @@ class Test extends AsyncResource {
525524
}
526525

527526
const { args, ctx } = this.getRunArgs();
528-
const after = runOnce(async () => {
529-
if (this.hooks.after.length > 0) {
530-
await this.runHook('after', { args, ctx });
531-
}
532-
});
527+
533528
const afterEach = runOnce(async () => {
534529
if (this.parent?.hooks.afterEach.length > 0) {
535530
await this.parent.runHook('afterEach', { args, ctx });
536531
}
537532
});
538533

539534
try {
540-
if (this.parent?.hooks.beforeEach.length > 0) {
541-
await this.parent.runHook('beforeEach', { args, ctx });
542-
}
543535
if (this.parent?.hooks.before.length > 0) {
544536
await this.parent.runHook('before', this.parent.getRunArgs());
545537
}
538+
if (this.parent?.hooks.beforeEach.length > 0) {
539+
await this.parent.runHook('beforeEach', { args, ctx });
540+
}
546541
const stopPromise = stopTest(this.timeout, this.signal);
547542
const runArgs = ArrayPrototypeSlice(args);
548543
ArrayPrototypeUnshift(runArgs, this.fn, ctx);
@@ -574,11 +569,17 @@ class Test extends AsyncResource {
574569
return;
575570
}
576571

577-
await after();
578572
await afterEach();
573+
if (this.hooks.after.length > 0) {
574+
await this.runHook('after', this.getRunArgs());
575+
}
579576
this.pass();
580577
} catch (err) {
581-
try { await after(); } catch { /* Ignore error. */ }
578+
try {
579+
if (this.hooks.after.length > 0) {
580+
await this.runHook('after', this.getRunArgs());
581+
}
582+
} catch { /* Ignore error. */ }
582583
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
583584
if (isTestFailureError(err)) {
584585
if (err.failureType === kTestTimeoutFailure) {

test/fixtures/test-runner/output/hooks.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,24 @@ test('test hooks', async (t) => {
9999
await t.test('2', () => testArr.push('2'));
100100

101101
await t.test('nested', async (t) => {
102+
t.before((t) => testArr.push('nested before ' + t.name));
103+
t.after((t) => testArr.push('nested after ' + t.name));
102104
t.beforeEach((t) => testArr.push('nested beforeEach ' + t.name));
103105
t.afterEach((t) => testArr.push('nested afterEach ' + t.name));
104106
await t.test('nested 1', () => testArr.push('nested1'));
105107
await t.test('nested 2', () => testArr.push('nested 2'));
106108
});
107109

108110
assert.deepStrictEqual(testArr, [
109-
'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
111+
'before test hooks',
112+
'beforeEach 1', '1', 'afterEach 1',
110113
'beforeEach 2', '2', 'afterEach 2',
111114
'beforeEach nested',
115+
'nested before nested',
112116
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
113117
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
114118
'afterEach nested',
119+
'nested after nested',
115120
]);
116121
});
117122

0 commit comments

Comments
 (0)