Skip to content

Commit

Permalink
test_runner: fix global after hook
Browse files Browse the repository at this point in the history
PR-URL: nodejs#48231
Fixes: nodejs#48230
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
MoLow authored and Ceres6 committed Aug 14, 2023
1 parent 63877ea commit f044963
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ function setup(root) {
const rejectionHandler =
createProcessEventHandler('unhandledRejection', root);
const coverage = configureCoverage(root, globalOptions);
const exitHandler = () => {
root.postRun(new ERR_TEST_FAILURE(
const exitHandler = async () => {
await root.run(new ERR_TEST_FAILURE(
'Promise resolution is still pending but the event loop has already resolved',
kCancelledByParent));

Expand All @@ -150,8 +150,8 @@ function setup(root) {
process.removeListener('uncaughtException', exceptionHandler);
};

const terminationHandler = () => {
exitHandler();
const terminationHandler = async () => {
await exitHandler();
process.exit();
};

Expand Down
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 () => {
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 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
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

0 comments on commit f044963

Please sign in to comment.