Skip to content

Commit 6c9fc93

Browse files
committed
test_runner: execute before hook on test
Execute the before hook for Test as well, and execute it on root before executing any tests. Fixes: nodejs#47518
1 parent 070c9a8 commit 6c9fc93

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

lib/internal/test_runner/test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ class TestContext {
127127
return subtest.start();
128128
}
129129

130+
before(fn, options) {
131+
this.#test.createHook('before', fn, options);
132+
}
133+
130134
after(fn, options) {
131135
this.#test.createHook('after', fn, options);
132136
}
@@ -414,6 +418,9 @@ class Test extends AsyncResource {
414418
validateOneOf(name, 'hook name', kHookNames);
415419
// eslint-disable-next-line no-use-before-define
416420
const hook = new TestHook(fn, options);
421+
if (name === 'before') {
422+
hook.run = runOnce(hook.run);
423+
}
417424
ArrayPrototypePush(this.hooks[name], hook);
418425
return hook;
419426
}
@@ -525,6 +532,9 @@ class Test extends AsyncResource {
525532
if (this.parent?.hooks.beforeEach.length > 0) {
526533
await this.parent.runHook('beforeEach', { args, ctx });
527534
}
535+
if (this.parent?.hooks.before.length > 0) {
536+
await this.parent.runHook('before', this.parent.getRunArgs());
537+
}
528538
const stopPromise = stopTest(this.timeout, this.signal);
529539
const runArgs = ArrayPrototypeSlice(args);
530540
ArrayPrototypeUnshift(runArgs, this.fn, ctx);
@@ -561,7 +571,7 @@ class Test extends AsyncResource {
561571
this.pass();
562572
} catch (err) {
563573
try { await after(); } catch { /* Ignore error. */ }
564-
try { await afterEach(); } catch { /* test is already failing, let's the error */ }
574+
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
565575
if (isTestFailureError(err)) {
566576
if (err.failureType === kTestTimeoutFailure) {
567577
this.#cancel(err);
@@ -793,7 +803,7 @@ class Suite extends Test {
793803

794804
this.pass();
795805
} catch (err) {
796-
try { await afterEach(); } catch { /* test is already failing, let's the error */ }
806+
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
797807
if (isTestFailureError(err)) {
798808
this.fail(err);
799809
} else {

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ describe('afterEach throws and test fails', () => {
9191
test('test hooks', async (t) => {
9292
const testArr = [];
9393

94+
t.before((t) => testArr.push('before ' + t.name));
9495
t.after(common.mustCall((t) => testArr.push('after ' + t.name)));
9596
t.beforeEach((t) => testArr.push('beforeEach ' + t.name));
9697
t.afterEach((t) => testArr.push('afterEach ' + t.name));
@@ -105,7 +106,7 @@ test('test hooks', async (t) => {
105106
});
106107

107108
assert.deepStrictEqual(testArr, [
108-
'beforeEach 1', '1', 'afterEach 1',
109+
'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
109110
'beforeEach 2', '2', 'afterEach 2',
110111
'beforeEach nested',
111112
'nested beforeEach nested 1', 'nested1', 'nested afterEach nested 1',
@@ -114,6 +115,13 @@ test('test hooks', async (t) => {
114115
]);
115116
});
116117

118+
test('t.before throws', async (t) => {
119+
t.after(common.mustCall());
120+
t.before(() => { throw new Error('before'); });
121+
await t.test('1', () => {});
122+
await t.test('2', () => {});
123+
});
124+
117125
test('t.beforeEach throws', async (t) => {
118126
t.after(common.mustCall());
119127
t.beforeEach(() => { throw new Error('beforeEach'); });

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

+56-8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ not ok 2 - before throws
6767
*
6868
*
6969
*
70+
*
7071
...
7172
# Subtest: after throws
7273
# Subtest: 1
@@ -307,6 +308,53 @@ ok 8 - test hooks
307308
---
308309
duration_ms: *
309310
...
311+
# Subtest: t.before throws
312+
# Subtest: 1
313+
not ok 1 - 1
314+
---
315+
duration_ms: *
316+
failureType: 'hookFailed'
317+
error: 'failed running before hook'
318+
code: 'ERR_TEST_FAILURE'
319+
stack: |-
320+
*
321+
*
322+
*
323+
*
324+
*
325+
*
326+
*
327+
*
328+
*
329+
*
330+
...
331+
# Subtest: 2
332+
not ok 2 - 2
333+
---
334+
duration_ms: *
335+
failureType: 'hookFailed'
336+
error: 'failed running before hook'
337+
code: 'ERR_TEST_FAILURE'
338+
stack: |-
339+
*
340+
*
341+
*
342+
*
343+
*
344+
*
345+
*
346+
*
347+
*
348+
*
349+
...
350+
1..2
351+
not ok 9 - t.before throws
352+
---
353+
duration_ms: *
354+
failureType: 'subtestsFailed'
355+
error: '2 subtests failed'
356+
code: 'ERR_TEST_FAILURE'
357+
...
310358
# Subtest: t.beforeEach throws
311359
# Subtest: 1
312360
not ok 1 - 1
@@ -347,7 +395,7 @@ ok 8 - test hooks
347395
*
348396
...
349397
1..2
350-
not ok 9 - t.beforeEach throws
398+
not ok 10 - t.beforeEach throws
351399
---
352400
duration_ms: *
353401
failureType: 'subtestsFailed'
@@ -394,7 +442,7 @@ not ok 9 - t.beforeEach throws
394442
*
395443
...
396444
1..2
397-
not ok 10 - t.afterEach throws
445+
not ok 11 - t.afterEach throws
398446
---
399447
duration_ms: *
400448
failureType: 'subtestsFailed'
@@ -427,7 +475,7 @@ not ok 10 - t.afterEach throws
427475
duration_ms: *
428476
...
429477
1..2
430-
not ok 11 - afterEach when test fails
478+
not ok 12 - afterEach when test fails
431479
---
432480
duration_ms: *
433481
failureType: 'subtestsFailed'
@@ -474,15 +522,15 @@ not ok 11 - afterEach when test fails
474522
*
475523
...
476524
1..2
477-
not ok 12 - afterEach throws and test fails
525+
not ok 13 - afterEach throws and test fails
478526
---
479527
duration_ms: *
480528
failureType: 'subtestsFailed'
481529
error: '2 subtests failed'
482530
code: 'ERR_TEST_FAILURE'
483531
...
484532
# Subtest: t.after() is called if test body throws
485-
not ok 13 - t.after() is called if test body throws
533+
not ok 14 - t.after() is called if test body throws
486534
---
487535
duration_ms: *
488536
failureType: 'testCodeFailure'
@@ -498,11 +546,11 @@ not ok 13 - t.after() is called if test body throws
498546
*
499547
...
500548
# - after() called
501-
1..13
502-
# tests 35
549+
1..14
550+
# tests 38
503551
# suites 8
504552
# pass 14
505-
# fail 19
553+
# fail 22
506554
# cancelled 2
507555
# skipped 0
508556
# todo 0

0 commit comments

Comments
 (0)