From 7ea9a406cb10e828099029c66671e7f5ff030572 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 20 Jan 2025 12:13:37 +0100 Subject: [PATCH] fix: update `@sinonjs/fake-timers` (#15470) --- CHANGELOG.md | 2 +- .../do-not-fake/__tests__/doNotFake.test.js | 2 +- packages/jest-fake-timers/package.json | 2 +- .../src/__tests__/modernFakeTimers.test.ts | 102 ++++++++++++++++++ yarn.lock | 14 +-- 5 files changed, 112 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e23fc489f62..6ea27e71e6d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ - `[jest-environment-node]` Update jest environment with dispose symbols `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888) & [#14909](https://github.com/jestjs/jest/pull/14909)) - `[expect, @jest/expect]` [**BREAKING**] Add type inference for function parameters in `CalledWith` assertions ([#15129](https://github.com/facebook/jest/pull/15129)) - `[@jest/expect-utils]` Properly compare all types of `TypedArray`s ([#15178](https://github.com/facebook/jest/pull/15178)) -- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544)) +- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v13 ([#14544](https://github.com/jestjs/jest/pull/14544) & [#15470](https://github.com/jestjs/jest/pull/15470)) - `[@jest/fake-timers]` Exposing new modern timers function `advanceTimersToFrame()` which advances all timers by the needed milliseconds to execute callbacks currently scheduled with `requestAnimationFrame` ([#14598](https://github.com/jestjs/jest/pull/14598)) - `[jest-matcher-utils]` Add `SERIALIZABLE_PROPERTIES` to allow custom serialization of objects ([#14893](https://github.com/jestjs/jest/pull/14893)) - `[jest-mock]` Add support for the Explicit Resource Management proposal to use the `using` keyword with `jest.spyOn(object, methodName)` ([#14895](https://github.com/jestjs/jest/pull/14895)) diff --git a/e2e/fake-timers/do-not-fake/__tests__/doNotFake.test.js b/e2e/fake-timers/do-not-fake/__tests__/doNotFake.test.js index c85eb58a0eeb..d7f28d46000a 100644 --- a/e2e/fake-timers/do-not-fake/__tests__/doNotFake.test.js +++ b/e2e/fake-timers/do-not-fake/__tests__/doNotFake.test.js @@ -15,7 +15,7 @@ globalThis.performance.mark = mockPerformanceMark; test('fakes all APIs', () => { jest.useFakeTimers(); - expect(globalThis.performance.mark).toBeUndefined(); + expect(globalThis.performance.mark).not.toBe(mockPerformanceMark); }); test('does not fake `performance` instance', () => { diff --git a/packages/jest-fake-timers/package.json b/packages/jest-fake-timers/package.json index a55a65d11e37..a61da1594ac1 100644 --- a/packages/jest-fake-timers/package.json +++ b/packages/jest-fake-timers/package.json @@ -20,7 +20,7 @@ }, "dependencies": { "@jest/types": "workspace:*", - "@sinonjs/fake-timers": "^11.1.0", + "@sinonjs/fake-timers": "^13.0.0", "@types/node": "*", "jest-message-util": "workspace:*", "jest-mock": "workspace:*", diff --git a/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts b/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts index 71a542c607da..e70222671511 100644 --- a/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts +++ b/packages/jest-fake-timers/src/__tests__/modernFakeTimers.test.ts @@ -14,8 +14,10 @@ describe('FakeTimers', () => { it('installs setTimeout mock', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -26,8 +28,10 @@ describe('FakeTimers', () => { it('installs clearTimeout mock', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -38,8 +42,10 @@ describe('FakeTimers', () => { it('installs setInterval mock', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -50,8 +56,10 @@ describe('FakeTimers', () => { it('installs clearInterval mock', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -63,10 +71,12 @@ describe('FakeTimers', () => { const origNextTick = () => {}; const global = { Date, + clearInterval, clearTimeout, process: { nextTick: origNextTick, }, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -78,9 +88,11 @@ describe('FakeTimers', () => { const origSetImmediate = () => {}; const global = { Date, + clearInterval, clearTimeout, process, setImmediate: origSetImmediate, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -94,9 +106,11 @@ describe('FakeTimers', () => { const global = { Date, clearImmediate: origClearImmediate, + clearInterval, clearTimeout, process, setImmediate: origSetImmediate, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -107,8 +121,10 @@ describe('FakeTimers', () => { it('mocks requestAnimationFrame if it exists on global', () => { const global = { Date, + clearInterval, clearTimeout, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -120,7 +136,9 @@ describe('FakeTimers', () => { const global = { Date, cancelAnimationFrame: () => {}, + clearInterval, clearTimeout, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -133,10 +151,12 @@ describe('FakeTimers', () => { it('runs all ticks, in order', () => { const global = { Date, + clearInterval, clearTimeout, process: { nextTick: () => {}, }, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -164,10 +184,12 @@ describe('FakeTimers', () => { const nextTick = jest.fn(); const global = { Date, + clearInterval, clearTimeout, process: { nextTick, }, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -181,10 +203,12 @@ describe('FakeTimers', () => { it('only runs a scheduled callback once', () => { const global = { Date, + clearInterval, clearTimeout, process: { nextTick: () => {}, }, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -205,10 +229,12 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = { Date, + clearInterval, clearTimeout, process: { nextTick: () => {}, }, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -235,8 +261,10 @@ describe('FakeTimers', () => { it('runs all timers in order', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -293,8 +321,10 @@ describe('FakeTimers', () => { const nativeSetTimeout = jest.fn(); const global = { Date, + clearInterval, clearTimeout, process, + setInterval: nativeSetTimeout, setTimeout: nativeSetTimeout, } as unknown as typeof globalThis; @@ -306,8 +336,10 @@ describe('FakeTimers', () => { it('only runs a setTimeout callback once (ever)', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -327,8 +359,10 @@ describe('FakeTimers', () => { it('runs callbacks with arguments after the interval', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -347,8 +381,10 @@ describe('FakeTimers', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval: nativeSetTimeout, setTimeout: nativeSetTimeout, } as unknown as typeof globalThis; @@ -369,8 +405,10 @@ describe('FakeTimers', () => { it('throws before allowing infinite recursion', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({ @@ -395,8 +433,10 @@ describe('FakeTimers', () => { it('also clears ticks', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -417,8 +457,10 @@ describe('FakeTimers', () => { it('runs timers in order', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -461,8 +503,10 @@ describe('FakeTimers', () => { it('does nothing when no timers have been scheduled', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -476,8 +520,10 @@ describe('FakeTimers', () => { it('runs timers in order', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -516,8 +562,10 @@ describe('FakeTimers', () => { it('run correct amount of steps', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -555,8 +603,10 @@ describe('FakeTimers', () => { it('setTimeout inside setTimeout', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -583,8 +633,10 @@ describe('FakeTimers', () => { it('does nothing when no timers have been scheduled', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -598,9 +650,11 @@ describe('FakeTimers', () => { it('runs scheduled animation frame callbacks in order', () => { const global = { Date, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -624,9 +678,11 @@ describe('FakeTimers', () => { it('should only run currently scheduled animation frame callbacks', () => { const global = { Date, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -657,9 +713,11 @@ describe('FakeTimers', () => { const global = { Date, cancelAnimationFrame: () => {}, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -679,9 +737,11 @@ describe('FakeTimers', () => { const global = { Date, cancelAnimationFrame: () => {}, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -713,9 +773,11 @@ describe('FakeTimers', () => { const global = { Date, cancelAnimationFrame: () => {}, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -739,9 +801,11 @@ describe('FakeTimers', () => { const global = { Date, cancelAnimationFrame: () => {}, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -769,10 +833,12 @@ describe('FakeTimers', () => { it('should call animation frame callbacks with the latest system time', () => { const global = { Date, + clearInterval, clearTimeout, performance, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -794,8 +860,10 @@ describe('FakeTimers', () => { it('resets all pending setTimeouts', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -812,8 +880,10 @@ describe('FakeTimers', () => { it('resets all pending setIntervals', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -830,11 +900,13 @@ describe('FakeTimers', () => { it('resets all pending ticks callbacks', () => { const global = { Date, + clearInterval, clearTimeout, process: { nextTick: () => {}, }, setImmediate: () => {}, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -852,8 +924,10 @@ describe('FakeTimers', () => { it('resets current advanceTimersByTime time cursor', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -873,9 +947,11 @@ describe('FakeTimers', () => { it('resets all scheduled animation frames', () => { const global = { Date, + clearInterval, clearTimeout, process, requestAnimationFrame: () => -1, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -896,9 +972,11 @@ describe('FakeTimers', () => { const global = { Date, + clearInterval, clearTimeout, process, setImmediate: nativeSetImmediate, + setInterval, setTimeout, } as unknown as typeof globalThis; @@ -963,8 +1041,10 @@ describe('FakeTimers', () => { it('does not run timers that were cleared in another timer', () => { const global = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1019,8 +1099,10 @@ describe('FakeTimers', () => { const global = { Date, + clearInterval, clearTimeout, process: {nextTick: nativeProcessNextTick}, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1042,9 +1124,11 @@ describe('FakeTimers', () => { const global = { Date, clearImmediate: nativeClearImmediate, + clearInterval, clearTimeout, process, setImmediate: nativeSetImmediate, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1100,8 +1184,10 @@ describe('FakeTimers', () => { const global = { Date, + clearInterval, clearTimeout, process: {nextTick: nativeProcessNextTick}, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1123,9 +1209,11 @@ describe('FakeTimers', () => { const global = { Date, clearImmediate: nativeClearImmediate, + clearInterval, clearTimeout, process, setImmediate: nativeSetImmediate, + setInterval, setTimeout, } as unknown as typeof globalThis; const fakeTimers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1149,9 +1237,11 @@ describe('FakeTimers', () => { beforeEach(() => { fakedGlobal = { Date, + clearInterval, clearTimeout, process, setImmediate, + setInterval, setTimeout, } as unknown as typeof globalThis; timers = new FakeTimers({ @@ -1204,8 +1294,10 @@ describe('FakeTimers', () => { const global = { Date, Promise, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1230,8 +1322,10 @@ describe('FakeTimers', () => { const global = { Date, Promise, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1256,8 +1350,10 @@ describe('FakeTimers', () => { const global = { Date, Promise, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1284,8 +1380,10 @@ describe('FakeTimers', () => { const global = { Date, Promise, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1312,8 +1410,10 @@ describe('FakeTimers', () => { const global = { Date, Promise, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; const timers = new FakeTimers({config: makeProjectConfig(), global}); @@ -1337,8 +1437,10 @@ describe('FakeTimers', () => { beforeEach(() => { fakedGlobal = { Date, + clearInterval, clearTimeout, process, + setInterval, setTimeout, } as unknown as typeof globalThis; timers = new FakeTimers({ diff --git a/yarn.lock b/yarn.lock index 48d09e55281e..236b33c54c21 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3710,7 +3710,7 @@ __metadata: dependencies: "@jest/test-utils": "workspace:*" "@jest/types": "workspace:*" - "@sinonjs/fake-timers": ^11.1.0 + "@sinonjs/fake-timers": ^13.0.0 "@types/node": "*" "@types/sinonjs__fake-timers": ^8.1.2 jest-message-util: "workspace:*" @@ -5286,7 +5286,7 @@ __metadata: languageName: node linkType: hard -"@sinonjs/commons@npm:^3.0.0": +"@sinonjs/commons@npm:^3.0.1": version: 3.0.1 resolution: "@sinonjs/commons@npm:3.0.1" dependencies: @@ -5295,12 +5295,12 @@ __metadata: languageName: node linkType: hard -"@sinonjs/fake-timers@npm:^11.1.0": - version: 11.2.2 - resolution: "@sinonjs/fake-timers@npm:11.2.2" +"@sinonjs/fake-timers@npm:^13.0.0": + version: 13.0.5 + resolution: "@sinonjs/fake-timers@npm:13.0.5" dependencies: - "@sinonjs/commons": ^3.0.0 - checksum: 68c29b0e1856fdc280df03ddbf57c726420b78e9f943a241b471edc018fb14ff36fdc1daafd6026cba08c3c7f50c976fb7ae11b88ff44cd7f609692ca7d25158 + "@sinonjs/commons": ^3.0.1 + checksum: b1c6ba87fadb7666d3aa126c9e8b4ac32b2d9e84c9e5fd074aa24cab3c8342fd655459de014b08e603be1e6c24c9f9716d76d6d2a36c50f59bb0091be61601dd languageName: node linkType: hard