| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -138,8 +138,8 @@ function setup(root) { | |||
| 138 | 138 | const rejectionHandler = | |
| 139 | 139 | createProcessEventHandler('unhandledRejection', root); | |
| 140 | 140 | const coverage = configureCoverage(root, globalOptions); | |
| 141 | - const exitHandler = () => { | ||
| 142 | - root.postRun(new ERR_TEST_FAILURE( | ||
| 141 | + const exitHandler = async () => { | ||
| 142 | + await root.run(new ERR_TEST_FAILURE( | ||
| 143 | 143 | 'Promise resolution is still pending but the event loop has already resolved', | |
| 144 | 144 | kCancelledByParent)); | |
| 145 | 145 | ||
@@ -148,8 +148,8 @@ function setup(root) { | |||
| 148 | 148 | process.removeListener('uncaughtException', exceptionHandler); | |
| 149 | 149 | }; | |
| 150 | 150 | ||
| 151 | - const terminationHandler = () => { | ||
| 152 | - exitHandler(); | ||
| 151 | + const terminationHandler = async () => { | ||
| 152 | + await exitHandler(); | ||
| 153 | 153 | process.exit(); | |
| 154 | 154 | }; | |
| 155 | 155 | ||
| 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) { | |
| 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')); | ||
| 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 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments