Skip to content

Commit

Permalink
test_runner: fix global before not called when no global test exists
Browse files Browse the repository at this point in the history
PR-URL: #48877
Backport-PR-URL: #49225
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
  • Loading branch information
rluvaton authored and UlisesGascon committed Sep 10, 2023
1 parent 25e967a commit 75333f3
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ class Suite extends Test {
return;
}

if (this.parent.hooks.before.length > 0) {
await this.parent.runHook('before', this.parent.getRunArgs());
}

await this.runHook('before', hookArgs);

const stopPromise = stopTest(this.timeout, this.signal);
Expand Down
84 changes: 84 additions & 0 deletions test/fixtures/test-runner/output/hooks-with-no-global-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';
const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test');
const assert = require("assert");

// This file should not have any global tests to reproduce bug #48844
const testArr = [];

before(() => testArr.push('global before'));
after(() => {
testArr.push('global after');

try {
assert.deepStrictEqual(testArr, [
'global before',
'describe before',

'describe beforeEach',
'describe it 1',
'describe afterEach',

'describe beforeEach',
'describe test 2',
'describe afterEach',

'describe nested before',

'describe beforeEach',
'describe nested beforeEach',
'describe nested it 1',
'describe afterEach',
'describe nested afterEach',

'describe beforeEach',
'describe nested beforeEach',
'describe nested test 2',
'describe afterEach',
'describe nested afterEach',

'describe nested after',
'describe after',
'global after',
]);
} catch (e) {
// TODO(rluvaton): remove the try catch after #48867 is fixed
console.error(e);
process.exit(1);
}
});

describe('describe hooks with no global tests', () => {
before(() => {
testArr.push('describe before');
});
after(()=> {
testArr.push('describe after');
});
beforeEach(() => {
testArr.push('describe beforeEach');
});
afterEach(() => {
testArr.push('describe afterEach');
});

it('1', () => testArr.push('describe it 1'));
test('2', () => testArr.push('describe test 2'));

describe('nested', () => {
before(() => {
testArr.push('describe nested before')
});
after(() => {
testArr.push('describe nested after')
});
beforeEach(() => {
testArr.push('describe nested beforeEach')
});
afterEach(() => {
testArr.push('describe nested afterEach')
});

it('nested 1', () => testArr.push('describe nested it 1'));
test('nested 2', () => testArr.push('describe nested test 2'));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
TAP version 13
# Subtest: describe hooks with no global tests
# Subtest: 1
ok 1 - 1
---
duration_ms: *
...
# Subtest: 2
ok 2 - 2
---
duration_ms: *
...
# Subtest: nested
# Subtest: nested 1
ok 1 - nested 1
---
duration_ms: *
...
# Subtest: nested 2
ok 2 - nested 2
---
duration_ms: *
...
1..2
ok 3 - nested
---
duration_ms: *
type: 'suite'
...
1..3
ok 1 - describe hooks with no global tests
---
duration_ms: *
type: 'suite'
...
1..1
# tests 4
# suites 2
# pass 4
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const tests = [
{ name: 'test-runner/output/describe_it.js' },
{ name: 'test-runner/output/describe_nested.js' },
{ name: 'test-runner/output/hooks.js' },
{ name: 'test-runner/output/hooks-with-no-global-test.js' },
{ name: 'test-runner/output/no_refs.js' },
{ name: 'test-runner/output/no_tests.js' },
{ name: 'test-runner/output/only_tests.js' },
Expand Down

0 comments on commit 75333f3

Please sign in to comment.