summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Nash <phillip.nash@sonarsource.com>2023-05-11 20:10:56 +1000
committerGitHub <noreply@github.com>2023-05-11 10:10:56 +0000
commit24615bd409d148c9ef2d03d6706d0807d5df4557 (patch)
tree8de3e53ba73d5dc93a290db286ddaf73a715aee0
parentea8fd2dfe0a234c6ea1ad245cc05dfe833f94adc (diff)
downloadnode-new-24615bd409d148c9ef2d03d6706d0807d5df4557.tar.gz
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 PR-URL: https://github.com/nodejs/node/pull/47931 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
-rw-r--r--lib/internal/test_runner/test.js8
-rw-r--r--test/fixtures/test-runner/output/hooks.js7
2 files changed, 10 insertions, 5 deletions
diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js
index 1f888bc627..083cc441ae 100644
--- a/lib/internal/test_runner/test.js
+++ b/lib/internal/test_runner/test.js
@@ -537,12 +537,12 @@ class Test extends AsyncResource {
});
try {
- if (this.parent?.hooks.beforeEach.length > 0) {
- await this.parent.runHook('beforeEach', { args, ctx });
- }
if (this.parent?.hooks.before.length > 0) {
await this.parent.runHook('before', this.parent.getRunArgs());
}
+ if (this.parent?.hooks.beforeEach.length > 0) {
+ await this.parent.runHook('beforeEach', { args, ctx });
+ }
const stopPromise = stopTest(this.timeout, this.signal);
const runArgs = ArrayPrototypeSlice(args);
ArrayPrototypeUnshift(runArgs, this.fn, ctx);
@@ -574,8 +574,8 @@ class Test extends AsyncResource {
return;
}
- await after();
await afterEach();
+ await after();
this.pass();
} catch (err) {
try { await after(); } catch { /* Ignore error. */ }
diff --git a/test/fixtures/test-runner/output/hooks.js b/test/fixtures/test-runner/output/hooks.js
index 30532a29ad..a69506bbda 100644
--- a/test/fixtures/test-runner/output/hooks.js
+++ b/test/fixtures/test-runner/output/hooks.js
@@ -99,6 +99,8 @@ test('test hooks', async (t) => {
await t.test('2', () => testArr.push('2'));
await t.test('nested', async (t) => {
+ t.before((t) => testArr.push('nested before ' + t.name));
+ t.after((t) => testArr.push('nested after ' + t.name));
t.beforeEach((t) => testArr.push('nested beforeEach ' + t.name));
t.afterEach((t) => testArr.push('nested afterEach ' + t.name));
await t.test('nested 1', () => testArr.push('nested1'));
@@ -106,12 +108,15 @@ test('test hooks', async (t) => {
});
assert.deepStrictEqual(testArr, [
- 'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
+ 'before test hooks',
+ 'beforeEach 1', '1', 'afterEach 1',
'beforeEach 2', '2', 'afterEach 2',
'beforeEach nested',
+ 'nested before nested',
'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
'afterEach nested',
+ 'nested after nested',
]);
});