GeezerVison is showing github.com — test_runner: preserve hook promise when executed twice · nodejs/node@b96868b
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/test.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,6 +91,8 @@ let kResistStopPropagation; | 91 | 91 | let findSourceMap; | 92 | 92 | let noopTestStream; | 93 | 93 |
| | 94 | +const kRunOnceOptions = { __proto__: null, preserveReturnValue: true }; | | 95 | + | 94 | 96 | function lazyFindSourceMap(file) { | 95 | 97 | if (findSourceMap === undefined) { | 96 | 98 | ({ findSourceMap } = require('internal/source_map/source_map_cache')); | @@ -567,7 +569,7 @@ class Test extends AsyncResource { | 567 | 569 | // eslint-disable-next-line no-use-before-define | 568 | 570 | const hook = new TestHook(fn, options); | 569 | 571 | if (name === 'before' || name === 'after') { | 570 | | - hook.run = runOnce(hook.run); | | 572 | + hook.run = runOnce(hook.run, kRunOnceOptions); | 571 | 573 | } | 572 | 574 | if (name === 'before' && this.startTime !== null) { | 573 | 575 | // Test has already started, run the hook immediately | @@ -710,7 +712,7 @@ class Test extends AsyncResource { | 710 | 712 | if (this.parent?.hooks.afterEach.length > 0 && !this.skipped) { | 711 | 713 | await this.parent.runHook('afterEach', hookArgs); | 712 | 714 | } | 713 | | - }); | | 715 | + }, kRunOnceOptions); | 714 | 716 |
| 715 | 717 | let stopPromise; | 716 | 718 |
| @@ -1081,7 +1083,7 @@ class Suite extends Test { | 1081 | 1083 | const hookArgs = this.getRunArgs(); | 1082 | 1084 |
| 1083 | 1085 | let stopPromise; | 1084 | | - const after = runOnce(() => this.runHook('after', hookArgs)); | | 1086 | + const after = runOnce(() => this.runHook('after', hookArgs), kRunOnceOptions); | 1085 | 1087 | try { | 1086 | 1088 | this.parent.activeSubtests++; | 1087 | 1089 | await this.buildSuite; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -514,12 +514,15 @@ function isInsideNodeModules() { | 514 | 514 | return false; | 515 | 515 | } | 516 | 516 |
| 517 | | -function once(callback) { | | 517 | +function once(callback, { preserveReturnValue = false } = kEmptyObject) { | 518 | 518 | let called = false; | | 519 | + let returnValue; | 519 | 520 | return function(...args) { | 520 | | - if (called) return; | | 521 | + if (called) return returnValue; | 521 | 522 | called = true; | 522 | | - return ReflectApply(callback, this, args); | | 523 | + const result = ReflectApply(callback, this, args); | | 524 | + returnValue = preserveReturnValue ? result : undefined; | | 525 | + return result; | 523 | 526 | }; | 524 | 527 | } | 525 | 528 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.js +50-1Lines changed: 50 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 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')); |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks.snapshot +19-3Lines changed: 19 additions & 3 deletions | 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 | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/test-runner/output/hooks_spec_reporter.snapshot +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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 | |
|