Skip to content

Commit c98b4b1

Browse files
xxzefghhi-ogawa
andauthored
fix(deps)!: update fake-timers to v14.0.0 (#7097)
Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent cb6db13 commit c98b4b1

File tree

10 files changed

+66
-60
lines changed

10 files changed

+66
-60
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
},
8484
"patchedDependencies": {
8585
"@types/[email protected]": "patches/@[email protected]",
86-
"@sinonjs/fake-timers@11.1.0": "patches/@sinonjs__fake-timers@11.1.0.patch",
86+
"@sinonjs/fake-timers@14.0.0": "patches/@sinonjs__fake-timers@14.0.0.patch",
8787
8888
"@types/[email protected]": "patches/@[email protected]",
8989

packages/vitest/LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Repository: git+https://github.com/sinonjs/commons.git
180180
## @sinonjs/fake-timers
181181
License: BSD-3-Clause
182182
By: Christian Johansen
183-
Repository: https://github.com/sinonjs/fake-timers.git
183+
Repository: git+https://github.com/sinonjs/fake-timers.git
184184

185185
> Copyright (c) 2010-2014, Christian Johansen, [email protected]. All rights reserved.
186186
>

packages/vitest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
"@ampproject/remapping": "^2.3.0",
176176
"@antfu/install-pkg": "^0.5.0",
177177
"@edge-runtime/vm": "^5.0.0",
178-
"@sinonjs/fake-timers": "11.1.0",
178+
"@sinonjs/fake-timers": "14.0.0",
179179
"@types/debug": "^4.1.12",
180180
"@types/estree": "^1.0.6",
181181
"@types/istanbul-lib-coverage": "^2.0.6",

packages/vitest/src/integrations/mock/timers.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,11 @@ export class FakeTimers {
158158
)
159159
}
160160

161-
// setImmediate/clearImmediate is not possible to mock when it's not globally avaiable and it throws an internal error.
162-
// this might be @sinonjs/fake-timers's bug and inconsistent behavior, but for now, we silently filter out these two beforehand for browser testing.
163-
// https://github.com/sinonjs/fake-timers/issues/277
164-
// https://github.com/sinonjs/sinon/issues/2085
165-
const existingFakedMethods = (this._userConfig?.toFake || toFake).filter(
166-
(method) => {
167-
switch (method) {
168-
case 'setImmediate':
169-
case 'clearImmediate':
170-
return method in this._global && this._global[method]
171-
default:
172-
return true
173-
}
174-
},
175-
)
176-
177161
this._clock = this._fakeTimers.install({
178162
now: Date.now(),
179163
...this._userConfig,
180-
toFake: existingFakedMethods,
164+
toFake: this._userConfig?.toFake || toFake,
165+
ignoreMissingTimers: true,
181166
})
182167

183168
this._fakingTime = true

packages/vitest/src/runtime/runVmTests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { VitestExecutor } from './execute'
44
import { createRequire } from 'node:module'
55
import { performance } from 'node:perf_hooks'
66
import timers from 'node:timers'
7+
import timersPromises from 'node:timers/promises'
78
import util from 'node:util'
89
import { collectTests, startTests } from '@vitest/runner'
910
import { KNOWN_ASSET_TYPES } from 'vite-node/constants'
@@ -56,6 +57,7 @@ export async function run(
5657
globalThis.__vitest_required__ = {
5758
util,
5859
timers,
60+
timersPromises,
5961
}
6062

6163
installSourcemapsSupport({

packages/vitest/src/runtime/setup-node.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { SerializedConfig } from './config'
33
import type { VitestExecutor } from './execute'
44
import { createRequire } from 'node:module'
55
import timers from 'node:timers'
6+
import timersPromises from 'node:timers/promises'
67
import util from 'node:util'
78
import { getSafeTimers } from '@vitest/utils'
89
import { KNOWN_ASSET_TYPES } from 'vite-node/constants'
@@ -61,6 +62,7 @@ export async function setupGlobalEnv(
6162
globalThis.__vitest_required__ = {
6263
util,
6364
timers,
65+
timersPromises,
6466
}
6567

6668
installSourcemapsSupport({
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js
2-
index 607336d6a9c568a32b0cde4499c8fd56f06d424a..35187b0ee298df858118494b5a9b3e5efa8197b0 100644
2+
index 11dab90bd4bafd8c3a232df20f82ec5bcf06e76d..1f633e6293bc4bff97ccf9a23214944c0f6f8395 100644
33
--- a/src/fake-timers-src.js
44
+++ b/src/fake-timers-src.js
5-
@@ -2,9 +2,9 @@
5+
@@ -2,14 +2,14 @@
66

77
const globalObject = require("@sinonjs/commons").global;
8-
let timersModule;
8+
let timersModule, timersPromisesModule;
99
-if (typeof require === "function" && typeof module === "object") {
1010
+if (typeof __vitest_required__ !== 'undefined') {
1111
try {
@@ -14,12 +14,18 @@ index 607336d6a9c568a32b0cde4499c8fd56f06d424a..35187b0ee298df858118494b5a9b3e5e
1414
} catch (e) {
1515
// ignored
1616
}
17-
@@ -159,7 +159,7 @@ function withGlobal(_global) {
18-
hrtimePresent && typeof _global.process.hrtime.bigint === "function";
19-
const nextTickPresent =
17+
try {
18+
- timersPromisesModule = require("timers/promises");
19+
+ timersPromisesModule = __vitest_required__.timersPromises;
20+
} catch (e) {
21+
// ignored
22+
}
23+
@@ -172,7 +172,7 @@ function withGlobal(_global) {
24+
isPresent.hrtime && typeof _global.process.hrtime.bigint === "function";
25+
isPresent.nextTick =
2026
_global.process && typeof _global.process.nextTick === "function";
2127
- const utilPromisify = _global.process && require("util").promisify;
2228
+ const utilPromisify = _global.process && _global.__vitest_required__ && _global.__vitest_required__.util.promisify;
23-
const performancePresent =
29+
isPresent.performance =
2430
_global.performance && typeof _global.performance.now === "function";
2531
const hasPerformancePrototype =

patches/@[email protected]

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/index.d.ts b/index.d.ts
2-
index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e2513016247058f37 100644
2+
index 5aa018cde4336aca4dadefb8338549c378792e14..1b8136e5fb4c6666a46dbef765c9624d62fdb3a5 100644
33
--- a/index.d.ts
44
+++ b/index.d.ts
55
@@ -329,13 +329,15 @@ export interface FakeTimerInstallOpts {
@@ -21,7 +21,7 @@ index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e25130162
2121
*/
2222
loopLimit?: number | undefined;
2323

24-
@@ -352,8 +354,8 @@ export interface FakeTimerInstallOpts {
24+
@@ -352,10 +354,16 @@ export interface FakeTimerInstallOpts {
2525
advanceTimeDelta?: number | undefined;
2626

2727
/**
@@ -31,4 +31,12 @@ index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e25130162
3131
+ * @default true
3232
*/
3333
shouldClearNativeTimers?: boolean | undefined;
34+
+
35+
+ /**
36+
+ * Don't throw error when asked to fake timers that are not present.
37+
+ * @default false
38+
+ */
39+
+ ignoreMissingTimers?: boolean | undefined;
3440
}
41+
42+
/**

pnpm-lock.yaml

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/core/test/fixtures/timers.suite.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ describe('FakeTimers', () => {
3838
})
3939

4040
it('installs setInterval mock', () => {
41-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
41+
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval }
4242
const timers = new FakeTimers({ global })
4343
timers.useFakeTimers()
4444
expect(global.setInterval).not.toBe(undefined)
4545
})
4646

4747
it('installs clearInterval mock', () => {
48-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
48+
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval }
4949
const timers = new FakeTimers({ global })
5050
timers.useFakeTimers()
5151
expect(global.clearInterval).not.toBe(undefined)
@@ -82,7 +82,7 @@ describe('FakeTimers', () => {
8282
})
8383

8484
it.runIf(isChildProcess)('throws when is child_process and tries to mock nextTick', () => {
85-
const global = { process, setTimeout, clearTimeout }
85+
const global = { Date: FakeDate, process, setTimeout, clearTimeout }
8686
const timers = new FakeTimers({ global, config: { toFake: ['nextTick'] } })
8787

8888
expect(() => timers.useFakeTimers()).toThrow(
@@ -120,11 +120,12 @@ describe('FakeTimers', () => {
120120
expect(global.clearImmediate).not.toBe(origClearImmediate)
121121
})
122122

123-
it('mocks requestIdleCallback even if not on global', () => {
124-
const global = { Date: FakeDate, clearTimeout, setTimeout };
125-
const timers = new FakeTimers({ global, config: { toFake: ["requestIdleCallback"] }})
123+
it('mocks requestIdleCallback if it exists on global', () => {
124+
const origRequestIdleCallback = () => {}
125+
const global = { Date: FakeDate, clearTimeout, setTimeout, requestIdleCallback: origRequestIdleCallback }
126+
const timers = new FakeTimers({ global })
126127
timers.useFakeTimers()
127-
expect(global.requestIdleCallback).toBeDefined();
128+
expect(global.requestIdleCallback).not.toBe(origRequestIdleCallback)
128129
})
129130

130131
it('cannot mock setImmediate and clearImmediate if not on global', () => {
@@ -237,7 +238,7 @@ describe('FakeTimers', () => {
237238

238239
describe('runAllTimers', () => {
239240
it('runs all timers in order', () => {
240-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
241+
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval }
241242
const timers = new FakeTimers({ global })
242243
timers.useFakeTimers()
243244

@@ -381,7 +382,7 @@ describe('FakeTimers', () => {
381382

382383
describe('runAllTimersAsync', () => {
383384
it('runs all timers in order', async () => {
384-
const global = { Date: FakeDate, clearTimeout, process, setTimeout, Promise }
385+
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval, Promise }
385386
const timers = new FakeTimers({ global })
386387
timers.useFakeTimers()
387388

@@ -511,7 +512,7 @@ describe('FakeTimers', () => {
511512

512513
describe('advanceTimersByTime', () => {
513514
it('runs timers in order', () => {
514-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
515+
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
515516
const timers = new FakeTimers({ global })
516517
timers.useFakeTimers()
517518

@@ -609,7 +610,7 @@ describe('FakeTimers', () => {
609610

610611
describe('advanceTimersToNextTimer', () => {
611612
it('runs timers in order', () => {
612-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
613+
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
613614
const timers = new FakeTimers({ global })
614615
timers.useFakeTimers()
615616

@@ -644,7 +645,7 @@ describe('FakeTimers', () => {
644645
})
645646

646647
it('run correct amount of steps', () => {
647-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
648+
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
648649
const timers = new FakeTimers({ global })
649650
timers.useFakeTimers()
650651

@@ -711,7 +712,7 @@ describe('FakeTimers', () => {
711712

712713
describe('advanceTimersToNextTimerAsync', () => {
713714
it('runs timers in order', async () => {
714-
const global = { Date: FakeDate, clearTimeout, process, setTimeout, Promise }
715+
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval, Promise }
715716
const timers = new FakeTimers({ global })
716717
timers.useFakeTimers()
717718

@@ -746,7 +747,7 @@ describe('FakeTimers', () => {
746747
})
747748

748749
it('run correct amount of steps', async () => {
749-
const global = { Date: FakeDate, clearTimeout, process, setTimeout, Promise }
750+
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval, Promise }
750751
const timers = new FakeTimers({ global })
751752
timers.useFakeTimers()
752753

@@ -1022,7 +1023,7 @@ describe('FakeTimers', () => {
10221023
})
10231024

10241025
it('resets all pending setIntervals', () => {
1025-
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
1026+
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
10261027
const timers = new FakeTimers({ global })
10271028
timers.useFakeTimers()
10281029

@@ -1083,6 +1084,7 @@ describe('FakeTimers', () => {
10831084
process,
10841085
setImmediate: nativeSetImmediate,
10851086
setTimeout,
1087+
setInterval,
10861088
}
10871089

10881090
const timers = new FakeTimers({ global })
@@ -1189,6 +1191,7 @@ describe('FakeTimers', () => {
11891191
process,
11901192
setImmediate,
11911193
setTimeout,
1194+
setInterval,
11921195
Promise,
11931196
}
11941197

0 commit comments

Comments
 (0)