| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f0e6acd commit 5d13419
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -151,7 +151,11 @@ function setup(root) { | |||
| 151 | 151 | const rejectionHandler = | |
| 152 | 152 | createProcessEventHandler('unhandledRejection', root); | |
| 153 | 153 | const coverage = configureCoverage(root, globalOptions); | |
| 154 | - const exitHandler = () => { | ||
| 154 | + const exitHandler = async () => { | ||
| 155 | + if (root.subtests.length === 0 && (root.hooks.before.length > 0 || root.hooks.after.length > 0)) { | ||
| 156 | + // Run global before/after hooks in case there are no tests | ||
| 157 | + await root.run(); | ||
| 158 | + } | ||
| 155 | 159 | root.postRun(new ERR_TEST_FAILURE( | |
| 156 | 160 | 'Promise resolution is still pending but the event loop has already resolved', | |
| 157 | 161 | kCancelledByParent)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -177,19 +177,23 @@ class TestContext { | |||
| 177 | 177 | } | |
| 178 | 178 | ||
| 179 | 179 | before(fn, options) { | |
| 180 | - this.#test.createHook('before', fn, options); | ||
| 180 | + this.#test | ||
| 181 | + .createHook('before', fn, { __proto__: null, ...options, hookType: 'before', loc: getCallerLocation() }); | ||
| 181 | 182 | } | |
| 182 | 183 | ||
| 183 | 184 | after(fn, options) { | |
| 184 | - this.#test.createHook('after', fn, options); | ||
| 185 | + this.#test | ||
| 186 | + .createHook('after', fn, { __proto__: null, ...options, hookType: 'after', loc: getCallerLocation() }); | ||
| 185 | 187 | } | |
| 186 | 188 | ||
| 187 | 189 | beforeEach(fn, options) { | |
| 188 | - this.#test.createHook('beforeEach', fn, options); | ||
| 190 | + this.#test | ||
| 191 | + .createHook('beforeEach', fn, { __proto__: null, ...options, hookType: 'beforeEach', loc: getCallerLocation() }); | ||
| 189 | 192 | } | |
| 190 | 193 | ||
| 191 | 194 | afterEach(fn, options) { | |
| 192 | - this.#test.createHook('afterEach', fn, options); | ||
| 195 | + this.#test | ||
| 196 | + .createHook('afterEach', fn, { __proto__: null, ...options, hookType: 'afterEach', loc: getCallerLocation() }); | ||
| 193 | 197 | } | |
| 194 | 198 | } | |
| 195 | 199 | ||
@@ -518,6 +522,14 @@ class Test extends AsyncResource { | |||
| 518 | 522 | if (name === 'before' || name === 'after') { | |
| 519 | 523 | hook.run = runOnce(hook.run); | |
| 520 | 524 | } | |
| 525 | + if (name === 'before' && this.startTime !== null) { | ||
| 526 | + // Test has already started, run the hook immediately | ||
| 527 | + PromisePrototypeThen(hook.run(this.getRunArgs()), () => { | ||
| 528 | + if (hook.error != null) { | ||
| 529 | + this.fail(hook.error); | ||
| 530 | + } | ||
| 531 | + }); | ||
| 532 | + } | ||
| 521 | 533 | ArrayPrototypePush(this.hooks[name], hook); | |
| 522 | 534 | return hook; | |
| 523 | 535 | } | |
@@ -615,26 +627,28 @@ class Test extends AsyncResource { | |||
| 615 | 627 | return; | |
| 616 | 628 | } | |
| 617 | 629 | ||
| 618 | - const { args, ctx } = this.getRunArgs(); | ||
| 630 | + const hookArgs = this.getRunArgs(); | ||
| 631 | + const { args, ctx } = hookArgs; | ||
| 619 | 632 | const after = async () => { | |
| 620 | 633 | if (this.hooks.after.length > 0) { | |
| 621 | - await this.runHook('after', { __proto__: null, args, ctx }); | ||
| 634 | + await this.runHook('after', hookArgs); | ||
| 622 | 635 | } | |
| 623 | 636 | }; | |
| 624 | 637 | const afterEach = runOnce(async () => { | |
| 625 | 638 | if (this.parent?.hooks.afterEach.length > 0) { | |
| 626 | - await this.parent.runHook('afterEach', { __proto__: null, args, ctx }); | ||
| 639 | + await this.parent.runHook('afterEach', hookArgs); | ||
| 627 | 640 | } | |
| 628 | 641 | }); | |
| 629 | 642 | ||
| 630 | 643 | let stopPromise; | |
| 631 | 644 | ||
| 632 | 645 | try { | |
| 633 | 646 | if (this.parent?.hooks.before.length > 0) { | |
| 647 | + // This hook usually runs immediately, we need to wait for it to finish | ||
| 634 | 648 | await this.parent.runHook('before', this.parent.getRunArgs()); | |
| 635 | 649 | } | |
| 636 | 650 | if (this.parent?.hooks.beforeEach.length > 0) { | |
| 637 | - await this.parent.runHook('beforeEach', { __proto__: null, args, ctx }); | ||
| 651 | + await this.parent.runHook('beforeEach', hookArgs); | ||
| 638 | 652 | } | |
| 639 | 653 | stopPromise = stopTest(this.timeout, this.signal); | |
| 640 | 654 | const runArgs = ArrayPrototypeSlice(args); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../../../common'); | ||
| 3 | + const { before, after } = require('node:test'); | ||
| 4 | + | ||
| 5 | + before(common.mustCall(() => console.log('before'))); | ||
| 6 | + after(common.mustCall(() => console.log('after'))); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + before | ||
| 2 | + TAP version 13 | ||
| 3 | + after | ||
| 4 | + 1..0 | ||
| 5 | + # tests 0 | ||
| 6 | + # suites 0 | ||
| 7 | + # pass 0 | ||
| 8 | + # fail 0 | ||
| 9 | + # cancelled 0 | ||
| 10 | + # skipped 0 | ||
| 11 | + # todo 0 | ||
| 12 | + # duration_ms * | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,7 @@ describe('describe hooks', () => { | |||
| 11 | 11 | before(function() { | |
| 12 | 12 | testArr.push('before ' + this.name); | |
| 13 | 13 | }); | |
| 14 | - after(function() { | ||
| 14 | + after(common.mustCall(function() { | ||
| 15 | 15 | testArr.push('after ' + this.name); | |
| 16 | 16 | assert.deepStrictEqual(testArr, [ | |
| 17 | 17 | 'before describe hooks', | |
@@ -23,7 +23,7 @@ describe('describe hooks', () => { | |||
| 23 | 23 | 'after nested', | |
| 24 | 24 | 'after describe hooks', | |
| 25 | 25 | ]); | |
| 26 | - }); | ||
| 26 | + })); | ||
| 27 | 27 | beforeEach(function() { | |
| 28 | 28 | testArr.push('beforeEach ' + this.name); | |
| 29 | 29 | }); | |
@@ -52,18 +52,43 @@ describe('describe hooks', () => { | |||
| 52 | 52 | }); | |
| 53 | 53 | }); | |
| 54 | 54 | ||
| 55 | + describe('describe hooks - no subtests', () => { | ||
| 56 | + const testArr = []; | ||
| 57 | + before(function() { | ||
| 58 | + testArr.push('before ' + this.name); | ||
| 59 | + }); | ||
| 60 | + after(common.mustCall(function() { | ||
| 61 | + testArr.push('after ' + this.name); | ||
| 62 | + assert.deepStrictEqual(testArr, [ | ||
| 63 | + 'before describe hooks - no subtests', | ||
| 64 | + 'after describe hooks - no subtests', | ||
| 65 | + ]); | ||
| 66 | + })); | ||
| 67 | + beforeEach(common.mustNotCall()); | ||
| 68 | + afterEach(common.mustNotCall()); | ||
| 69 | + }); | ||
| 70 | + | ||
| 55 | 71 | describe('before throws', () => { | |
| 56 | 72 | before(() => { throw new Error('before'); }); | |
| 57 | 73 | it('1', () => {}); | |
| 58 | 74 | test('2', () => {}); | |
| 59 | 75 | }); | |
| 60 | 76 | ||
| 77 | + describe('before throws - no subtests', () => { | ||
| 78 | + before(() => { throw new Error('before'); }); | ||
| 79 | + after(common.mustCall()); | ||
| 80 | + }); | ||
| 81 | + | ||
| 61 | 82 | describe('after throws', () => { | |
| 62 | 83 | after(() => { throw new Error('after'); }); | |
| 63 | 84 | it('1', () => {}); | |
| 64 | 85 | test('2', () => {}); | |
| 65 | 86 | }); | |
| 66 | 87 | ||
| 88 | + describe('after throws - no subtests', () => { | ||
| 89 | + after(() => { throw new Error('after'); }); | ||
| 90 | + }); | ||
| 91 | + | ||
| 67 | 92 | describe('beforeEach throws', () => { | |
| 68 | 93 | beforeEach(() => { throw new Error('beforeEach'); }); | |
| 69 | 94 | it('1', () => {}); | |
@@ -123,13 +148,48 @@ test('test hooks', async (t) => { | |||
| 123 | 148 | })); | |
| 124 | 149 | }); | |
| 125 | 150 | ||
| 151 | + | ||
| 152 | + test('test hooks - no subtests', async (t) => { | ||
| 153 | + const testArr = []; | ||
| 154 | + | ||
| 155 | + t.before((t) => testArr.push('before ' + t.name)); | ||
| 156 | + t.after(common.mustCall((t) => testArr.push('after ' + t.name))); | ||
| 157 | + t.beforeEach(common.mustNotCall()); | ||
| 158 | + t.afterEach(common.mustNotCall()); | ||
| 159 | + | ||
| 160 | + t.after(common.mustCall(() => { | ||
| 161 | + assert.deepStrictEqual(testArr, [ | ||
| 162 | + 'before test hooks - no subtests', | ||
| 163 | + 'after test hooks - no subtests', | ||
| 164 | + ]); | ||
| 165 | + })); | ||
| 166 | + }); | ||
| 167 | + | ||
| 126 | 168 | test('t.before throws', async (t) => { | |
| 127 | 169 | t.after(common.mustCall()); | |
| 128 | 170 | t.before(() => { throw new Error('before'); }); | |
| 129 | 171 | await t.test('1', () => {}); | |
| 130 | 172 | await t.test('2', () => {}); | |
| 131 | 173 | }); | |
| 132 | 174 | ||
| 175 | + test('t.before throws - no subtests', async (t) => { | ||
| 176 | + t.after(common.mustCall()); | ||
| 177 | + t.before(() => { throw new Error('before'); }); | ||
| 178 | + }); | ||
| 179 | + | ||
| 180 | + test('t.after throws', async (t) => { | ||
| 181 | + t.before(common.mustCall()); | ||
| 182 | + t.after(() => { throw new Error('after'); }); | ||
| 183 | + await t.test('1', () => {}); | ||
| 184 | + await t.test('2', () => {}); | ||
| 185 | + }); | ||
| 186 | + | ||
| 187 | + test('t.after throws - no subtests', async (t) => { | ||
| 188 | + t.before(common.mustCall()); | ||
| 189 | + t.after(() => { throw new Error('after'); }); | ||
| 190 | + }); | ||
| 191 | + | ||
| 192 | + | ||
| 133 | 193 | test('t.beforeEach throws', async (t) => { | |
| 134 | 194 | t.after(common.mustCall()); | |
| 135 | 195 | t.beforeEach(() => { throw new Error('beforeEach'); }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments