Skip to content

Commit a0f3ed8

Browse files
authored
test_runner: fix global before not called when no global test exists
PR-URL: #48877 Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent ad0603e commit a0f3ed8

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

lib/internal/test_runner/test.js

+4
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,10 @@ class Suite extends Test {
828828
return;
829829
}
830830

831+
if (this.parent.hooks.before.length > 0) {
832+
await this.parent.runHook('before', this.parent.getRunArgs());
833+
}
834+
831835
await this.runHook('before', hookArgs);
832836

833837
const stopPromise = stopTest(this.timeout, this.signal);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test');
3+
const assert = require("assert");
4+
5+
// This file should not have any global tests to reproduce bug #48844
6+
const testArr = [];
7+
8+
before(() => testArr.push('global before'));
9+
after(() => {
10+
testArr.push('global after');
11+
12+
try {
13+
assert.deepStrictEqual(testArr, [
14+
'global before',
15+
'describe before',
16+
17+
'describe beforeEach',
18+
'describe it 1',
19+
'describe afterEach',
20+
21+
'describe beforeEach',
22+
'describe test 2',
23+
'describe afterEach',
24+
25+
'describe nested before',
26+
27+
'describe beforeEach',
28+
'describe nested beforeEach',
29+
'describe nested it 1',
30+
'describe afterEach',
31+
'describe nested afterEach',
32+
33+
'describe beforeEach',
34+
'describe nested beforeEach',
35+
'describe nested test 2',
36+
'describe afterEach',
37+
'describe nested afterEach',
38+
39+
'describe nested after',
40+
'describe after',
41+
'global after',
42+
]);
43+
} catch (e) {
44+
// TODO(rluvaton): remove the try catch after #48867 is fixed
45+
console.error(e);
46+
process.exit(1);
47+
}
48+
});
49+
50+
describe('describe hooks with no global tests', () => {
51+
before(() => {
52+
testArr.push('describe before');
53+
});
54+
after(()=> {
55+
testArr.push('describe after');
56+
});
57+
beforeEach(() => {
58+
testArr.push('describe beforeEach');
59+
});
60+
afterEach(() => {
61+
testArr.push('describe afterEach');
62+
});
63+
64+
it('1', () => testArr.push('describe it 1'));
65+
test('2', () => testArr.push('describe test 2'));
66+
67+
describe('nested', () => {
68+
before(() => {
69+
testArr.push('describe nested before')
70+
});
71+
after(() => {
72+
testArr.push('describe nested after')
73+
});
74+
beforeEach(() => {
75+
testArr.push('describe nested beforeEach')
76+
});
77+
afterEach(() => {
78+
testArr.push('describe nested afterEach')
79+
});
80+
81+
it('nested 1', () => testArr.push('describe nested it 1'));
82+
test('nested 2', () => testArr.push('describe nested test 2'));
83+
});
84+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
TAP version 13
2+
# Subtest: describe hooks with no global tests
3+
# Subtest: 1
4+
ok 1 - 1
5+
---
6+
duration_ms: *
7+
...
8+
# Subtest: 2
9+
ok 2 - 2
10+
---
11+
duration_ms: *
12+
...
13+
# Subtest: nested
14+
# Subtest: nested 1
15+
ok 1 - nested 1
16+
---
17+
duration_ms: *
18+
...
19+
# Subtest: nested 2
20+
ok 2 - nested 2
21+
---
22+
duration_ms: *
23+
...
24+
1..2
25+
ok 3 - nested
26+
---
27+
duration_ms: *
28+
type: 'suite'
29+
...
30+
1..3
31+
ok 1 - describe hooks with no global tests
32+
---
33+
duration_ms: *
34+
type: 'suite'
35+
...
36+
1..1
37+
# tests 4
38+
# suites 2
39+
# pass 4
40+
# fail 0
41+
# cancelled 0
42+
# skipped 0
43+
# todo 0
44+
# duration_ms *

test/parallel/test-runner-output.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const tests = [
3636
{ name: 'test-runner/output/describe_it.js' },
3737
{ name: 'test-runner/output/describe_nested.js' },
3838
{ name: 'test-runner/output/hooks.js' },
39+
{ name: 'test-runner/output/hooks-with-no-global-test.js' },
3940
{ name: 'test-runner/output/no_refs.js' },
4041
{ name: 'test-runner/output/no_tests.js' },
4142
{ name: 'test-runner/output/only_tests.js' },

0 commit comments

Comments
 (0)