test_runner: fix global after hook by MoLow · Pull Request #48231 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/internal/test_runner/harness.js
12 changes: 6 additions & 6 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class Test extends AsyncResource {
validateOneOf(name, 'hook name', kHookNames);
// eslint-disable-next-line no-use-before-define
const hook = new TestHook(fn, options);
if (name === 'before') {
if (name === 'before' || name === 'after') {
hook.run = runOnce(hook.run);
}
ArrayPrototypePush(this.hooks[name], hook);
Expand Down Expand Up @@ -514,7 +514,7 @@ class Test extends AsyncResource {
}
}

async run() {
async run(pendingSubtestsError) {
if (this.parent !== null) {
this.parent.activeSubtests++;
}
Expand All @@ -526,11 +526,11 @@ class Test extends AsyncResource {
}

const { args, ctx } = this.getRunArgs();
const after = runOnce(async () => {
Comment thread
cjihrig marked this conversation as resolved.
const after = async () => {
if (this.hooks.after.length > 0) {
await this.runHook('after', { args, ctx });
}
});
};
const afterEach = runOnce(async () => {
if (this.parent?.hooks.afterEach.length > 0) {
await this.parent.runHook('afterEach', { args, ctx });
Expand Down Expand Up @@ -579,8 +579,8 @@ class Test extends AsyncResource {
await after();
this.pass();
} catch (err) {
try { await after(); } catch { /* Ignore error. */ }
try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ }
try { await after(); } catch { /* Ignore error. */ }
Comment thread
MoLow marked this conversation as resolved.
if (isTestFailureError(err)) {
if (err.failureType === kTestTimeoutFailure) {
this.#cancel(err);
Expand All @@ -594,7 +594,7 @@ class Test extends AsyncResource {

// Clean up the test. Then, try to report the results and execute any
// tests that were pending due to available concurrency.
this.postRun();
this.postRun(pendingSubtestsError);
}

postRun(pendingSubtestsError) {
Expand Down
27 changes: 16 additions & 11 deletions test/fixtures/test-runner/output/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const assert = require('assert');
const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test');

before((t) => t.diagnostic('before 1 called'));
after((t) => t.diagnostic('after 1 called'));

describe('describe hooks', () => {
const testArr = [];
Expand Down Expand Up @@ -107,17 +108,20 @@ test('test hooks', async (t) => {
await t.test('nested 2', () => testArr.push('nested 2'));
});

assert.deepStrictEqual(testArr, [
'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',
]);
t.after(common.mustCall(() => {
assert.deepStrictEqual(testArr, [
'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',
'after test hooks',
]);
}));
});

test('t.before throws', async (t) => {
Expand Down Expand Up @@ -164,3 +168,4 @@ test('t.after() is called if test body throws', (t) => {
});

before((t) => t.diagnostic('before 2 called'));
after((t) => t.diagnostic('after 2 called'));
3 changes: 3 additions & 0 deletions test/fixtures/test-runner/output/hooks.snapshot