Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fix global after hook #48231

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ function setup(root) {
const rejectionHandler =
createProcessEventHandler('unhandledRejection', root);
const coverage = configureCoverage(root, globalOptions);
const exitHandler = () => {
const exitHandler = async () => {
try {
if (root.hooks.after.length > 0) {
await root.runHook('after', root.getRunArgs());
}
} catch { /* Ignore error. */ }
root.postRun(new ERR_TEST_FAILURE(
'Promise resolution is still pending but the event loop has already resolved',
kCancelledByParent));
Expand Down
8 changes: 4 additions & 4 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 @@ -526,11 +526,11 @@ class Test extends AsyncResource {
}

const { args, ctx } = this.getRunArgs();
const after = runOnce(async () => {
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. */ }
if (isTestFailureError(err)) {
if (err.failureType === kTestTimeoutFailure) {
this.#cancel(err);
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ not ok 3 - after throws
*
*
*
*
...
# Subtest: beforeEach throws
# Subtest: 1
Expand Down Expand Up @@ -544,6 +545,8 @@ not ok 14 - t.after() is called if test body throws
1..14
# before 1 called
# before 2 called
# after 1 called
# after 2 called
# tests 38
# suites 8
# pass 14
Expand Down