| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a5f3dd1 commit cf817e1
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -87,6 +87,8 @@ const { | |||
| 87 | 87 | let kResistStopPropagation; | |
| 88 | 88 | let findSourceMap; | |
| 89 | 89 | ||
| 90 | + const kRunOnceOptions = { __proto__: null, preserveReturnValue: true }; | ||
| 91 | + | ||
| 90 | 92 | function lazyFindSourceMap(file) { | |
| 91 | 93 | if (findSourceMap === undefined) { | |
| 92 | 94 | ({ findSourceMap } = require('internal/source_map/source_map_cache')); | |
@@ -526,7 +528,7 @@ class Test extends AsyncResource { | |||
| 526 | 528 | // eslint-disable-next-line no-use-before-define | |
| 527 | 529 | const hook = new TestHook(fn, options); | |
| 528 | 530 | if (name === 'before' || name === 'after') { | |
| 529 | - hook.run = runOnce(hook.run); | ||
| 531 | + hook.run = runOnce(hook.run, kRunOnceOptions); | ||
| 530 | 532 | } | |
| 531 | 533 | if (name === 'before' && this.startTime !== null) { | |
| 532 | 534 | // Test has already started, run the hook immediately | |
@@ -650,7 +652,7 @@ class Test extends AsyncResource { | |||
| 650 | 652 | if (this.parent?.hooks.afterEach.length > 0 && !this.skipped) { | |
| 651 | 653 | await this.parent.runHook('afterEach', hookArgs); | |
| 652 | 654 | } | |
| 653 | - }); | ||
| 655 | + }, kRunOnceOptions); | ||
| 654 | 656 | ||
| 655 | 657 | let stopPromise; | |
| 656 | 658 | ||
@@ -1004,7 +1006,7 @@ class Suite extends Test { | |||
| 1004 | 1006 | const hookArgs = this.getRunArgs(); | |
| 1005 | 1007 | ||
| 1006 | 1008 | let stopPromise; | |
| 1007 | - const after = runOnce(() => this.runHook('after', hookArgs)); | ||
| 1009 | + const after = runOnce(() => this.runHook('after', hookArgs), kRunOnceOptions); | ||
| 1008 | 1010 | try { | |
| 1009 | 1011 | this.parent.activeSubtests++; | |
| 1010 | 1012 | await this.buildSuite; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -511,12 +511,15 @@ function isInsideNodeModules() { | |||
| 511 | 511 | return false; | |
| 512 | 512 | } | |
| 513 | 513 | ||
| 514 | - function once(callback) { | ||
| 514 | + function once(callback, { preserveReturnValue = false } = kEmptyObject) { | ||
| 515 | 515 | let called = false; | |
| 516 | + let returnValue; | ||
| 516 | 517 | return function(...args) { | |
| 517 | - if (called) return; | ||
| 518 | + if (called) return returnValue; | ||
| 518 | 519 | called = true; | |
| 519 | - return ReflectApply(callback, this, args); | ||
| 520 | + const result = ReflectApply(callback, this, args); | ||
| 521 | + returnValue = preserveReturnValue ? result : undefined; | ||
| 522 | + return result; | ||
| 520 | 523 | }; | |
| 521 | 524 | } | |
| 522 | 525 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | const common = require('../../../common'); | |
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test'); | |
| 5 | + const { setTimeout } = require('node:timers/promises'); | ||
| 5 | 6 | ||
| 6 | 7 | before((t) => t.diagnostic('before 1 called')); | |
| 7 | 8 | after((t) => t.diagnostic('after 1 called')); | |
@@ -148,7 +149,6 @@ test('test hooks', async (t) => { | |||
| 148 | 149 | })); | |
| 149 | 150 | }); | |
| 150 | 151 | ||
| 151 | - | ||
| 152 | 152 | test('test hooks - no subtests', async (t) => { | |
| 153 | 153 | const testArr = []; | |
| 154 | 154 | ||
@@ -253,5 +253,54 @@ describe('run after when before throws', () => { | |||
| 253 | 253 | it('1', () => {}); | |
| 254 | 254 | }); | |
| 255 | 255 | ||
| 256 | + | ||
| 257 | + test('test hooks - async', async (t) => { | ||
| 258 | + const testArr = []; | ||
| 259 | + | ||
| 260 | + t.before(async (t) => { | ||
| 261 | + testArr.push('before starting ' + t.name); | ||
| 262 | + await setTimeout(10); | ||
| 263 | + testArr.push('before ending ' + t.name); | ||
| 264 | + }); | ||
| 265 | + t.after(async (t) => { | ||
| 266 | + testArr.push('after starting ' + t.name); | ||
| 267 | + await setTimeout(10); | ||
| 268 | + testArr.push('after ending ' + t.name); | ||
| 269 | + }); | ||
| 270 | + t.beforeEach(async (t) => { | ||
| 271 | + testArr.push('beforeEach starting ' + t.name); | ||
| 272 | + await setTimeout(10); | ||
| 273 | + testArr.push('beforeEach ending ' + t.name); | ||
| 274 | + }); | ||
| 275 | + t.afterEach(async (t) => { | ||
| 276 | + testArr.push('afterEach starting ' + t.name); | ||
| 277 | + await setTimeout(10); | ||
| 278 | + testArr.push('afterEach ending ' + t.name); | ||
| 279 | + }); | ||
| 280 | + await t.test('1', async () => { | ||
| 281 | + testArr.push('1 starting'); | ||
| 282 | + await setTimeout(10); | ||
| 283 | + testArr.push('1 ending'); | ||
| 284 | + }); | ||
| 285 | + await t.test('2', async () => { | ||
| 286 | + testArr.push('2 starting'); | ||
| 287 | + await setTimeout(10); | ||
| 288 | + testArr.push('2 ending'); | ||
| 289 | + }); | ||
| 290 | + | ||
| 291 | + t.after(common.mustCall(() => { | ||
| 292 | + assert.deepStrictEqual(testArr, [ | ||
| 293 | + 'before starting test hooks - async', 'before ending test hooks - async', | ||
| 294 | + 'beforeEach starting 1', 'beforeEach ending 1', | ||
| 295 | + '1 starting', '1 ending', | ||
| 296 | + 'afterEach starting 1', 'afterEach ending 1', | ||
| 297 | + 'beforeEach starting 2', 'beforeEach ending 2', | ||
| 298 | + '2 starting', '2 ending', | ||
| 299 | + 'afterEach starting 2', 'afterEach ending 2', | ||
| 300 | + 'after starting test hooks - async', 'after ending test hooks - async', | ||
| 301 | + ]); | ||
| 302 | + })); | ||
| 303 | + }); | ||
| 304 | + | ||
| 256 | 305 | before((t) => t.diagnostic('before 2 called')); | |
| 257 | 306 | after((t) => t.diagnostic('after 2 called')); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -767,14 +767,30 @@ not ok 24 - run after when before throws | |||
| 767 | 767 | * | |
| 768 | 768 | * | |
| 769 | 769 | ... | |
| 770 | - 1..24 | ||
| 770 | + # Subtest: test hooks - async | ||
| 771 | + # Subtest: 1 | ||
| 772 | + ok 1 - 1 | ||
| 773 | + --- | ||
| 774 | + duration_ms: * | ||
| 775 | + ... | ||
| 776 | + # Subtest: 2 | ||
| 777 | + ok 2 - 2 | ||
| 778 | + --- | ||
| 779 | + duration_ms: * | ||
| 780 | + ... | ||
| 781 | + 1..2 | ||
| 782 | + ok 25 - test hooks - async | ||
| 783 | + --- | ||
| 784 | + duration_ms: * | ||
| 785 | + ... | ||
| 786 | + 1..25 | ||
| 771 | 787 | # before 1 called | |
| 772 | 788 | # before 2 called | |
| 773 | 789 | # after 1 called | |
| 774 | 790 | # after 2 called | |
| 775 | - # tests 49 | ||
| 791 | + # tests 52 | ||
| 776 | 792 | # suites 12 | |
| 777 | - # pass 19 | ||
| 793 | + # pass 22 | ||
| 778 | 794 | # fail 27 | |
| 779 | 795 | # cancelled 3 | |
| 780 | 796 | # skipped 0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -392,13 +392,17 @@ | |||
| 392 | 392 | * | |
| 393 | 393 | * | |
| 394 | 394 | ||
| 395 | + test hooks - async | ||
| 396 | + 1 (*ms) | ||
| 397 | + 2 (*ms) | ||
| 398 | + test hooks - async (*ms) | ||
| 395 | 399 | before 1 called | |
| 396 | 400 | before 2 called | |
| 397 | 401 | after 1 called | |
| 398 | 402 | after 2 called | |
| 399 | - tests 49 | ||
| 403 | + tests 52 | ||
| 400 | 404 | suites 12 | |
| 401 | - pass 19 | ||
| 405 | + pass 22 | ||
| 402 | 406 | fail 27 | |
| 403 | 407 | cancelled 3 | |
| 404 | 408 | skipped 0 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments