GeezerVison is showing github.com — test_runner: fix global after hook · nodejs/node@f3268d6
File treeExpand file treeCollapse file tree Open diff view settings test/fixtures/test-runner/output
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/harness.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,8 +140,8 @@ function setup(root) { | 140 | 140 | const rejectionHandler = | 141 | 141 | createProcessEventHandler('unhandledRejection', root); | 142 | 142 | const coverage = configureCoverage(root, globalOptions); | 143 | | - const exitHandler = () => { | 144 | | - root.postRun(new ERR_TEST_FAILURE( | | 143 | + const exitHandler = async () => { | | 144 | + await root.run(new ERR_TEST_FAILURE( | 145 | 145 | 'Promise resolution is still pending but the event loop has already resolved', | 146 | 146 | kCancelledByParent)); | 147 | 147 |
| @@ -150,8 +150,8 @@ function setup(root) { | 150 | 150 | process.removeListener('uncaughtException', exceptionHandler); | 151 | 151 | }; | 152 | 152 |
| 153 | | - const terminationHandler = () => { | 154 | | - exitHandler(); | | 153 | + const terminationHandler = async () => { | | 154 | + await exitHandler(); | 155 | 155 | process.exit(); | 156 | 156 | }; | 157 | 157 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/test.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -427,7 +427,7 @@ class Test extends AsyncResource { | 427 | 427 | validateOneOf(name, 'hook name', kHookNames); | 428 | 428 | // eslint-disable-next-line no-use-before-define | 429 | 429 | const hook = new TestHook(fn, options); | 430 | | - if (name === 'before') { | | 430 | + if (name === 'before' || name === 'after') { | 431 | 431 | hook.run = runOnce(hook.run); | 432 | 432 | } | 433 | 433 | ArrayPrototypePush(this.hooks[name], hook); | @@ -514,7 +514,7 @@ class Test extends AsyncResource { | 514 | 514 | } | 515 | 515 | } | 516 | 516 |
| 517 | | - async run() { | | 517 | + async run(pendingSubtestsError) { | 518 | 518 | if (this.parent !== null) { | 519 | 519 | this.parent.activeSubtests++; | 520 | 520 | } | @@ -526,11 +526,11 @@ class Test extends AsyncResource { | 526 | 526 | } | 527 | 527 |
| 528 | 528 | const { args, ctx } = this.getRunArgs(); | 529 | | - const after = runOnce(async () => { | | 529 | + const after = async () => { | 530 | 530 | if (this.hooks.after.length > 0) { | 531 | 531 | await this.runHook('after', { args, ctx }); | 532 | 532 | } | 533 | | - }); | | 533 | + }; | 534 | 534 | const afterEach = runOnce(async () => { | 535 | 535 | if (this.parent?.hooks.afterEach.length > 0) { | 536 | 536 | await this.parent.runHook('afterEach', { args, ctx }); | @@ -579,8 +579,8 @@ class Test extends AsyncResource { | 579 | 579 | await after(); | 580 | 580 | this.pass(); | 581 | 581 | } catch (err) { | 582 | | - try { await after(); } catch { /* Ignore error. */ } | 583 | 582 | try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ } | | 583 | + try { await after(); } catch { /* Ignore error. */ } | 584 | 584 | if (isTestFailureError(err)) { | 585 | 585 | if (err.failureType === kTestTimeoutFailure) { | 586 | 586 | this.#cancel(err); | @@ -594,7 +594,7 @@ class Test extends AsyncResource { | 594 | 594 |
| 595 | 595 | // Clean up the test. Then, try to report the results and execute any | 596 | 596 | // tests that were pending due to available concurrency. | 597 | | - this.postRun(); | | 597 | + this.postRun(pendingSubtestsError); | 598 | 598 | } | 599 | 599 |
| 600 | 600 | postRun(pendingSubtestsError) { | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.js +16-11Lines changed: 16 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ const assert = require('assert'); | 5 | 5 | const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test'); | 6 | 6 |
| 7 | 7 | before((t) => t.diagnostic('before 1 called')); | | 8 | +after((t) => t.diagnostic('after 1 called')); | 8 | 9 |
| 9 | 10 | describe('describe hooks', () => { | 10 | 11 | const testArr = []; | @@ -107,17 +108,20 @@ test('test hooks', async (t) => { | 107 | 108 | await t.test('nested 2', () => testArr.push('nested 2')); | 108 | 109 | }); | 109 | 110 |
| 110 | | - assert.deepStrictEqual(testArr, [ | 111 | | - 'before test hooks', | 112 | | - 'beforeEach 1', '1', 'afterEach 1', | 113 | | - 'beforeEach 2', '2', 'afterEach 2', | 114 | | - 'beforeEach nested', | 115 | | - 'nested before nested', | 116 | | - 'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1', | 117 | | - 'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2', | 118 | | - 'afterEach nested', | 119 | | - 'nested after nested', | 120 | | - ]); | | 111 | + t.after(common.mustCall(() => { | | 112 | + assert.deepStrictEqual(testArr, [ | | 113 | + 'before test hooks', | | 114 | + 'beforeEach 1', '1', 'afterEach 1', | | 115 | + 'beforeEach 2', '2', 'afterEach 2', | | 116 | + 'beforeEach nested', | | 117 | + 'nested before nested', | | 118 | + 'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1', | | 119 | + 'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2', | | 120 | + 'afterEach nested', | | 121 | + 'nested after nested', | | 122 | + 'after test hooks', | | 123 | + ]); | | 124 | + })); | 121 | 125 | }); | 122 | 126 |
| 123 | 127 | test('t.before throws', async (t) => { | @@ -164,3 +168,4 @@ test('t.after() is called if test body throws', (t) => { | 164 | 168 | }); | 165 | 169 |
| 166 | 170 | before((t) => t.diagnostic('before 2 called')); | | 171 | +after((t) => t.diagnostic('after 2 called')); |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.snapshot +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -97,6 +97,7 @@ not ok 3 - after throws | 97 | 97 | * | 98 | 98 | * | 99 | 99 | * | | 100 | + * | 100 | 101 | ... | 101 | 102 | # Subtest: beforeEach throws | 102 | 103 | # Subtest: 1 | @@ -544,6 +545,8 @@ not ok 14 - t.after() is called if test body throws | 544 | 545 | 1..14 | 545 | 546 | # before 1 called | 546 | 547 | # before 2 called | | 548 | +# after 1 called | | 549 | +# after 2 called | 547 | 550 | # tests 38 | 548 | 551 | # suites 8 | 549 | 552 | # pass 14 | |
|