| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 54ebfc1 commit e2c3b01
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -370,7 +370,7 @@ class MockTimers { | |||
| 370 | 370 | return MockDate; | |
| 371 | 371 | } | |
| 372 | 372 | ||
| 373 | - async * #setIntervalPromisified(interval, startTime, options) { | ||
| 373 | + async * #setIntervalPromisified(interval, result, options) { | ||
| 374 | 374 | const context = this; | |
| 375 | 375 | const emitter = new EventEmitter(); | |
| 376 | 376 | if (options?.signal) { | |
@@ -394,8 +394,7 @@ class MockTimers { | |||
| 394 | 394 | ||
| 395 | 395 | const eventIt = EventEmitter.on(emitter, 'data'); | |
| 396 | 396 | const callback = () => { | |
| 397 | - startTime += interval; | ||
| 398 | - emitter.emit('data', startTime); | ||
| 397 | + emitter.emit('data', result); | ||
| 399 | 398 | }; | |
| 400 | 399 | ||
| 401 | 400 | const timerId = this.#createTimer(true, callback, interval, options); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -407,6 +407,16 @@ describe('Mock Timers Test Suite', () => { | |||
| 407 | 407 | assert.strictEqual(result, expectedResult); | |
| 408 | 408 | }); | |
| 409 | 409 | ||
| 410 | + it('should always return the same result as the original timers/promises/setTimeout', async (t) => { | ||
| 411 | + t.mock.timers.enable({ apis: ['setTimeout'] }); | ||
| 412 | + for (const expectedResult of [undefined, null, false, true, 0, 0n, 1, 1n, '', 'result', {}]) { | ||
| 413 | + const p = nodeTimersPromises.setTimeout(2000, expectedResult); | ||
| 414 | + t.mock.timers.tick(2000); | ||
| 415 | + const result = await p; | ||
| 416 | + assert.strictEqual(result, expectedResult); | ||
| 417 | + } | ||
| 418 | + }); | ||
| 419 | + | ||
| 410 | 420 | it('should abort operation if timers/promises/setTimeout received an aborted signal', async (t) => { | |
| 411 | 421 | t.mock.timers.enable({ apis: ['setTimeout'] }); | |
| 412 | 422 | const expectedResult = 'result'; | |
@@ -505,10 +515,11 @@ describe('Mock Timers Test Suite', () => { | |||
| 505 | 515 | ||
| 506 | 516 | const expectedIterations = 5; | |
| 507 | 517 | const interval = 1000; | |
| 508 | - const startedAt = Date.now(); | ||
| 518 | + let time = 0; | ||
| 509 | 519 | async function run() { | |
| 510 | 520 | const times = []; | |
| 511 | - for await (const time of nodeTimersPromises.setInterval(interval, startedAt)) { | ||
| 521 | + for await (const _ of nodeTimersPromises.setInterval(interval)) { // eslint-disable-line no-unused-vars | ||
| 522 | + time += interval; | ||
| 512 | 523 | times.push(time); | |
| 513 | 524 | if (times.length === expectedIterations) break; | |
| 514 | 525 | } | |
@@ -525,7 +536,20 @@ describe('Mock Timers Test Suite', () => { | |||
| 525 | 536 | const timeResults = await r; | |
| 526 | 537 | assert.strictEqual(timeResults.length, expectedIterations); | |
| 527 | 538 | for (let it = 1; it < expectedIterations; it++) { | |
| 528 | - assert.strictEqual(timeResults[it - 1], startedAt + (interval * it)); | ||
| 539 | + assert.strictEqual(timeResults[it - 1], interval * it); | ||
| 540 | + } | ||
| 541 | + }); | ||
| 542 | + | ||
| 543 | + it('should always return the same result as the original timers/promises/setInterval', async (t) => { | ||
| 544 | + t.mock.timers.enable({ apis: ['setInterval'] }); | ||
| 545 | + for (const expectedResult of [undefined, null, false, true, 0, 0n, 1, 1n, '', 'result', {}]) { | ||
| 546 | + const intervalIterator = nodeTimersPromises.setInterval(2000, expectedResult); | ||
| 547 | + const p = intervalIterator.next(); | ||
| 548 | + t.mock.timers.tick(2000); | ||
| 549 | + const result = await p; | ||
| 550 | + await intervalIterator.return(); | ||
| 551 | + assert.strictEqual(result.done, false); | ||
| 552 | + assert.strictEqual(result.value, expectedResult); | ||
| 529 | 553 | } | |
| 530 | 554 | }); | |
| 531 | 555 | ||
@@ -579,13 +603,12 @@ describe('Mock Timers Test Suite', () => { | |||
| 579 | 603 | const signal = controller.signal; | |
| 580 | 604 | const interval = 200; | |
| 581 | 605 | const expectedIterations = 2; | |
| 582 | - const startedAt = Date.now(); | ||
| 583 | - const timeResults = []; | ||
| 606 | + let numIterations = 0; | ||
| 584 | 607 | async function run() { | |
| 585 | - const it = nodeTimersPromises.setInterval(interval, startedAt, { signal }); | ||
| 586 | - for await (const time of it) { | ||
| 587 | - timeResults.push(time); | ||
| 588 | - if (timeResults.length === 5) break; | ||
| 608 | + const it = nodeTimersPromises.setInterval(interval, undefined, { signal }); | ||
| 609 | + for await (const _ of it) { // eslint-disable-line no-unused-vars | ||
| 610 | + numIterations += 1; | ||
| 611 | + if (numIterations === 5) break; | ||
| 589 | 612 | } | |
| 590 | 613 | } | |
| 591 | 614 | ||
@@ -601,11 +624,7 @@ describe('Mock Timers Test Suite', () => { | |||
| 601 | 624 | await assert.rejects(() => r, { | |
| 602 | 625 | name: 'AbortError', | |
| 603 | 626 | }); | |
| 604 | - assert.strictEqual(timeResults.length, expectedIterations); | ||
| 605 | - | ||
| 606 | - for (let it = 1; it < expectedIterations; it++) { | ||
| 607 | - assert.strictEqual(timeResults[it - 1], startedAt + (interval * it)); | ||
| 608 | - } | ||
| 627 | + assert.strictEqual(numIterations, expectedIterations); | ||
| 609 | 628 | }); | |
| 610 | 629 | }); | |
| 611 | 630 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments