Skip to content

Commit 689f340

Browse files
committed
fix(jest-fake-timers): update sinon to v15
this updates to v15 and adds patches to the timers.
1 parent 7cf3b8d commit 689f340

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
diff --git a/index.d.ts b/index.d.ts
2+
index 5aa018cde4336aca4dadefb8338549c378792e14..957eba93020b6406bce77e0020e7502bd17993f2 100644
3+
--- a/index.d.ts
4+
+++ b/index.d.ts
5+
@@ -77,6 +77,11 @@ export interface GlobalTimers<TTimerId extends TimerId> {
6+
* Implements the Date object but using this clock to provide the correct time.
7+
*/
8+
Date: typeof Date;
9+
+
10+
+ /**
11+
+ * Mimics process.nextTick() explicitly dropping additional arguments.
12+
+ */
13+
+ queueMicrotask: (callback: () => void) => void;
14+
}
15+
16+
/**
17+
@@ -104,6 +109,19 @@ export interface NodeTimer {
18+
*/
19+
export type TimerId = number | NodeTimer;
20+
21+
+
22+
+/**
23+
+ * Allows configuring how the clock advances time, automatically or manually.
24+
+ *
25+
+ * - `manual`: Timers do not advance without explicit, manual calls to the tick APIs (`clock.nextAsync`, `clock.runAllAsync`, etc).
26+
+ * - `nextAsync`: The clock will continuously break the event loop, then run the next timer until the mode changes.
27+
+ * - `interval`: This is the same as specifying `shouldAdvanceTime: true` with an `advanceTimeDelta`. If the delta is not specified, 20 will be used by default.
28+
+ */
29+
+export type TimerTickMode =
30+
+ | { mode: "manual" }
31+
+ | { mode: "nextAsync" }
32+
+ | { mode: "interval"; delta?: number };
33+
+
34+
/**
35+
* Controls the flow of time.
36+
*/
37+
@@ -253,6 +271,17 @@ export interface FakeClock<TTimerId extends TimerId> extends GlobalTimers<TTimer
38+
* @remarks This affects the current time but it does not in itself cause timers to fire.
39+
*/
40+
setSystemTime: (now?: number | Date) => void;
41+
+
42+
+ /**
43+
+ * Allows configuring how the clock advances time, automatically or manually.
44+
+ * @param tickModeConfig The new configuration for how the clock should tick.
45+
+ */
46+
+ setTickMode: (tickModeConfig: TimerTickMode) => void;
47+
+
48+
+ /**
49+
+ * Run all pending microtasks scheduled with nextTick.
50+
+ */
51+
+ runMicrotasks: () => void;
52+
}
53+
54+
/**
55+
@@ -281,11 +310,6 @@ export type NodeClock = FakeClock<NodeTimer> & {
56+
* Simulates process.nextTick().
57+
*/
58+
nextTick: (callback: (...args: any[]) => void, ...args: any[]) => void;
59+
-
60+
- /**
61+
- * Run all pending microtasks scheduled with nextTick.
62+
- */
63+
- runMicrotasks: () => void;
64+
};
65+
66+
/**

packages/jest-fake-timers/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
},
2121
"dependencies": {
2222
"@jest/types": "workspace:*",
23-
"@sinonjs/fake-timers": "^13.0.0",
23+
"@sinonjs/fake-timers": "^15.0.0",
2424
"@types/node": "*",
2525
"jest-message-util": "workspace:*",
2626
"jest-mock": "workspace:*",
2727
"jest-util": "workspace:*"
2828
},
2929
"devDependencies": {
3030
"@jest/test-utils": "workspace:*",
31-
"@types/sinonjs__fake-timers": "^8.1.5"
31+
"@types/sinonjs__fake-timers": "15.0.0"
3232
},
3333
"engines": {
3434
"node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"

packages/jest-fake-timers/src/modernFakeTimers.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default class FakeTimers {
118118

119119
runAllTicks(): void {
120120
if (this._checkFakeTimers()) {
121-
// @ts-expect-error - doesn't exist?
121+
// @ts-expect-error needs an upstream fix: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/73943
122122
this._clock.runMicrotasks();
123123
}
124124
}
@@ -156,6 +156,15 @@ export default class FakeTimers {
156156
}
157157
}
158158

159+
setTimerTickMode(tickModeConfig: {
160+
mode: 'interval' | 'manual' | 'nextAsync';
161+
delta?: number;
162+
}): void {
163+
if (this._checkFakeTimers()) {
164+
this._clock.setTickMode(tickModeConfig);
165+
}
166+
}
167+
159168
getRealSystemTime(): number {
160169
return Date.now();
161170
}

yarn.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,9 +4385,9 @@ __metadata:
43854385
dependencies:
43864386
"@jest/test-utils": "workspace:*"
43874387
"@jest/types": "workspace:*"
4388-
"@sinonjs/fake-timers": "npm:^13.0.0"
4388+
"@sinonjs/fake-timers": "npm:^15.0.0"
43894389
"@types/node": "npm:*"
4390-
"@types/sinonjs__fake-timers": "npm:^8.1.5"
4390+
"@types/sinonjs__fake-timers": "npm:15.0.0"
43914391
jest-message-util: "workspace:*"
43924392
jest-mock: "workspace:*"
43934393
jest-util: "workspace:*"
@@ -6061,12 +6061,12 @@ __metadata:
60616061
languageName: node
60626062
linkType: hard
60636063

6064-
"@sinonjs/fake-timers@npm:^13.0.0":
6065-
version: 13.0.5
6066-
resolution: "@sinonjs/fake-timers@npm:13.0.5"
6064+
"@sinonjs/fake-timers@npm:^15.0.0":
6065+
version: 15.0.0
6066+
resolution: "@sinonjs/fake-timers@npm:15.0.0"
60676067
dependencies:
60686068
"@sinonjs/commons": "npm:^3.0.1"
6069-
checksum: 10/11ee417968fc4dce1896ab332ac13f353866075a9d2a88ed1f6258f17cc4f7d93e66031b51fcddb8c203aa4d53fd980b0ae18aba06269f4682164878a992ec3f
6069+
checksum: 10/ae9e6fc11ec9e053d4658e276d8b5fac3204b62870f1610feb837baf86d73c604d1464004668e385093d46c22b54eb7f1db6c64593216fadf8c9c501fc2537d4
60706070
languageName: node
60716071
linkType: hard
60726072

@@ -7038,10 +7038,10 @@ __metadata:
70387038
languageName: node
70397039
linkType: hard
70407040

7041-
"@types/sinonjs__fake-timers@npm:^8.1.5":
7042-
version: 8.1.5
7043-
resolution: "@types/sinonjs__fake-timers@npm:8.1.5"
7044-
checksum: 10/3a0b285fcb8e1eca435266faa27ffff206608b69041022a42857274e44d9305822e85af5e7a43a9fae78d2ab7dc0fcb49f3ae3bda1fa81f0203064dbf5afd4f6
7041+
"@types/sinonjs__fake-timers@npm:15.0.0":
7042+
version: 15.0.0
7043+
resolution: "@types/sinonjs__fake-timers@npm:15.0.0"
7044+
checksum: 10/ca16587fdeed07c9fab84cbca54b844763046f6a2e21de3664ea75c417b372a3288ad339d396119fd167cd9e42ada90758ce9a5b372124261886e69749cdf223
70457045
languageName: node
70467046
linkType: hard
70477047

0 commit comments

Comments
 (0)