Skip to content

Commit

Permalink
fix(deps)!: update fake-timers to v14.0.0 (#7097)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshi Ogawa <[email protected]>
  • Loading branch information
xxzefgh and hi-ogawa authored Dec 31, 2024
1 parent cb6db13 commit c98b4b1
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
"patchedDependencies": {
"@types/[email protected]": "patches/@[email protected]",
"@sinonjs/fake-timers@11.1.0": "patches/@sinonjs__fake-timers@11.1.0.patch",
"@sinonjs/fake-timers@14.0.0": "patches/@sinonjs__fake-timers@14.0.0.patch",
"[email protected]": "patches/[email protected]",
"@types/[email protected]": "patches/@[email protected]",
"[email protected]": "patches/[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Repository: git+https://github.com/sinonjs/commons.git
## @sinonjs/fake-timers
License: BSD-3-Clause
By: Christian Johansen
Repository: https://github.com/sinonjs/fake-timers.git
Repository: git+https://github.com/sinonjs/fake-timers.git

> Copyright (c) 2010-2014, Christian Johansen, [email protected]. All rights reserved.
>
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
"@ampproject/remapping": "^2.3.0",
"@antfu/install-pkg": "^0.5.0",
"@edge-runtime/vm": "^5.0.0",
"@sinonjs/fake-timers": "11.1.0",
"@sinonjs/fake-timers": "14.0.0",
"@types/debug": "^4.1.12",
"@types/estree": "^1.0.6",
"@types/istanbul-lib-coverage": "^2.0.6",
Expand Down
19 changes: 2 additions & 17 deletions packages/vitest/src/integrations/mock/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,11 @@ export class FakeTimers {
)
}

// setImmediate/clearImmediate is not possible to mock when it's not globally avaiable and it throws an internal error.
// this might be @sinonjs/fake-timers's bug and inconsistent behavior, but for now, we silently filter out these two beforehand for browser testing.
// https://github.com/sinonjs/fake-timers/issues/277
// https://github.com/sinonjs/sinon/issues/2085
const existingFakedMethods = (this._userConfig?.toFake || toFake).filter(
(method) => {
switch (method) {
case 'setImmediate':
case 'clearImmediate':
return method in this._global && this._global[method]
default:
return true
}
},
)

this._clock = this._fakeTimers.install({
now: Date.now(),
...this._userConfig,
toFake: existingFakedMethods,
toFake: this._userConfig?.toFake || toFake,
ignoreMissingTimers: true,
})

this._fakingTime = true
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/runtime/runVmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { VitestExecutor } from './execute'
import { createRequire } from 'node:module'
import { performance } from 'node:perf_hooks'
import timers from 'node:timers'
import timersPromises from 'node:timers/promises'
import util from 'node:util'
import { collectTests, startTests } from '@vitest/runner'
import { KNOWN_ASSET_TYPES } from 'vite-node/constants'
Expand Down Expand Up @@ -56,6 +57,7 @@ export async function run(
globalThis.__vitest_required__ = {
util,
timers,
timersPromises,
}

installSourcemapsSupport({
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/runtime/setup-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { SerializedConfig } from './config'
import type { VitestExecutor } from './execute'
import { createRequire } from 'node:module'
import timers from 'node:timers'
import timersPromises from 'node:timers/promises'
import util from 'node:util'
import { getSafeTimers } from '@vitest/utils'
import { KNOWN_ASSET_TYPES } from 'vite-node/constants'
Expand Down Expand Up @@ -61,6 +62,7 @@ export async function setupGlobalEnv(
globalThis.__vitest_required__ = {
util,
timers,
timersPromises,
}

installSourcemapsSupport({
Expand Down
20 changes: 13 additions & 7 deletions patches/@[email protected] → patches/@[email protected]
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js
index 607336d6a9c568a32b0cde4499c8fd56f06d424a..35187b0ee298df858118494b5a9b3e5efa8197b0 100644
index 11dab90bd4bafd8c3a232df20f82ec5bcf06e76d..1f633e6293bc4bff97ccf9a23214944c0f6f8395 100644
--- a/src/fake-timers-src.js
+++ b/src/fake-timers-src.js
@@ -2,9 +2,9 @@
@@ -2,14 +2,14 @@

const globalObject = require("@sinonjs/commons").global;
let timersModule;
let timersModule, timersPromisesModule;
-if (typeof require === "function" && typeof module === "object") {
+if (typeof __vitest_required__ !== 'undefined') {
try {
Expand All @@ -14,12 +14,18 @@ index 607336d6a9c568a32b0cde4499c8fd56f06d424a..35187b0ee298df858118494b5a9b3e5e
} catch (e) {
// ignored
}
@@ -159,7 +159,7 @@ function withGlobal(_global) {
hrtimePresent && typeof _global.process.hrtime.bigint === "function";
const nextTickPresent =
try {
- timersPromisesModule = require("timers/promises");
+ timersPromisesModule = __vitest_required__.timersPromises;
} catch (e) {
// ignored
}
@@ -172,7 +172,7 @@ function withGlobal(_global) {
isPresent.hrtime && typeof _global.process.hrtime.bigint === "function";
isPresent.nextTick =
_global.process && typeof _global.process.nextTick === "function";
- const utilPromisify = _global.process && require("util").promisify;
+ const utilPromisify = _global.process && _global.__vitest_required__ && _global.__vitest_required__.util.promisify;
const performancePresent =
isPresent.performance =
_global.performance && typeof _global.performance.now === "function";
const hasPerformancePrototype =
12 changes: 10 additions & 2 deletions patches/@[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/index.d.ts b/index.d.ts
index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e2513016247058f37 100644
index 5aa018cde4336aca4dadefb8338549c378792e14..1b8136e5fb4c6666a46dbef765c9624d62fdb3a5 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -329,13 +329,15 @@ export interface FakeTimerInstallOpts {
Expand All @@ -21,7 +21,7 @@ index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e25130162
*/
loopLimit?: number | undefined;

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

/**
Expand All @@ -31,4 +31,12 @@ index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e25130162
+ * @default true
*/
shouldClearNativeTimers?: boolean | undefined;
+
+ /**
+ * Don't throw error when asked to fake timers that are not present.
+ * @default false
+ */
+ ignoreMissingTimers?: boolean | undefined;
}

/**
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 18 additions & 15 deletions test/core/test/fixtures/timers.suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ describe('FakeTimers', () => {
})

it('installs setInterval mock', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()
expect(global.setInterval).not.toBe(undefined)
})

it('installs clearInterval mock', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()
expect(global.clearInterval).not.toBe(undefined)
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('FakeTimers', () => {
})

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

expect(() => timers.useFakeTimers()).toThrow(
Expand Down Expand Up @@ -120,11 +120,12 @@ describe('FakeTimers', () => {
expect(global.clearImmediate).not.toBe(origClearImmediate)
})

it('mocks requestIdleCallback even if not on global', () => {
const global = { Date: FakeDate, clearTimeout, setTimeout };
const timers = new FakeTimers({ global, config: { toFake: ["requestIdleCallback"] }})
it('mocks requestIdleCallback if it exists on global', () => {
const origRequestIdleCallback = () => {}
const global = { Date: FakeDate, clearTimeout, setTimeout, requestIdleCallback: origRequestIdleCallback }
const timers = new FakeTimers({ global })
timers.useFakeTimers()
expect(global.requestIdleCallback).toBeDefined();
expect(global.requestIdleCallback).not.toBe(origRequestIdleCallback)
})

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

describe('runAllTimers', () => {
it('runs all timers in order', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -381,7 +382,7 @@ describe('FakeTimers', () => {

describe('runAllTimersAsync', () => {
it('runs all timers in order', async () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout, Promise }
const global = { Date: FakeDate, clearTimeout, clearInterval, process, setTimeout, setInterval, Promise }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -511,7 +512,7 @@ describe('FakeTimers', () => {

describe('advanceTimersByTime', () => {
it('runs timers in order', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -609,7 +610,7 @@ describe('FakeTimers', () => {

describe('advanceTimersToNextTimer', () => {
it('runs timers in order', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -644,7 +645,7 @@ describe('FakeTimers', () => {
})

it('run correct amount of steps', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -711,7 +712,7 @@ describe('FakeTimers', () => {

describe('advanceTimersToNextTimerAsync', () => {
it('runs timers in order', async () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout, Promise }
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval, Promise }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -746,7 +747,7 @@ describe('FakeTimers', () => {
})

it('run correct amount of steps', async () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout, Promise }
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval, Promise }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -1022,7 +1023,7 @@ describe('FakeTimers', () => {
})

it('resets all pending setIntervals', () => {
const global = { Date: FakeDate, clearTimeout, process, setTimeout }
const global = { Date: FakeDate, clearTimeout, process, setTimeout, setInterval }
const timers = new FakeTimers({ global })
timers.useFakeTimers()

Expand Down Expand Up @@ -1083,6 +1084,7 @@ describe('FakeTimers', () => {
process,
setImmediate: nativeSetImmediate,
setTimeout,
setInterval,
}

const timers = new FakeTimers({ global })
Expand Down Expand Up @@ -1189,6 +1191,7 @@ describe('FakeTimers', () => {
process,
setImmediate,
setTimeout,
setInterval,
Promise,
}

Expand Down

0 comments on commit c98b4b1

Please sign in to comment.