From 7764a1e3ba7ec6eb7b64f8e3e1d5e5e47d6eca9f Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 14 Feb 2024 11:08:35 +0900 Subject: [PATCH 1/3] docs: addd default `fakeTimers.toFake` --- docs/api/vi.md | 2 +- docs/config/index.md | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index 7d0ab77ac6ce..eb2d419fac65 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -738,7 +738,7 @@ vi.useRealTimers() - **Type:** `() => Vitest` -To enable mocking timers, you need to call this method. It will wrap all further calls to timers (such as `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate`, `clearImmediate`, and `Date`), until [`vi.useRealTimers()`](#vi-userealtimers) is called. +To enable mocking timers, you need to call this method. It will wrap all further calls to timers (such as `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `setImmediate`, `clearImmediate`, and `Date`), until [`vi.useRealTimers()`](#vi-userealtimers) is called. Mocking `nextTick` is not supported when running Vitest inside `node:child_process` by using `--pool=forks`. NodeJS uses `process.nextTick` internally in `node:child_process` and hangs when it is mocked. Mocking `nextTick` is supported when running Vitest with `--pool=threads`. diff --git a/docs/config/index.md b/docs/config/index.md index 18ebf912a88a..537deabba7a7 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2035,9 +2035,10 @@ Installs fake timers with the specified Unix epoch. #### fakeTimers.toFake -- **Type:** `FakeMethod[]` +- **Type:** `('setTimeout' | 'clearTimeout' | 'setImmediate' | 'clearImmediate' | 'setInterval' | 'clearInterval' | 'Date' | 'nextTick' | 'hrtime' | 'requestAnimationFrame' | 'cancelAnimationFrame' | 'requestIdleCallback' | 'cancelIdleCallback' | 'performance' | 'queueMicrotask')[]` +- **Default:** `['setTimeout', 'clearTimeout', 'setImmediate', 'clearImmediate', 'setInterval', 'clearInterval', 'Date']` -An array with names of global methods and APIs to fake. By default, Vitest does not replace `nextTick()` and `queueMicrotask()`. +An array with names of global methods and APIs to fake. To only mock `setTimeout()` and `nextTick()`, specify this property as `['setTimeout', 'nextTick']`. From e631a639e24152b66d957739f8f2b353baa89643 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 14 Feb 2024 11:19:57 +0900 Subject: [PATCH 2/3] docs: patch @types__sinonjs__fake-timers to update jsdoc --- package.json | 3 +- .../@types__sinonjs__fake-timers@8.1.5.patch | 34 +++++++++++++++++++ pnpm-lock.yaml | 8 +++-- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 patches/@types__sinonjs__fake-timers@8.1.5.patch diff --git a/package.json b/package.json index baed07ecabd6..5e6b449e7c4c 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,8 @@ "patchedDependencies": { "@types/chai@4.3.6": "patches/@types__chai@4.3.6.patch", "@sinonjs/fake-timers@11.1.0": "patches/@sinonjs__fake-timers@11.1.0.patch", - "cac@6.7.14": "patches/cac@6.7.14.patch" + "cac@6.7.14": "patches/cac@6.7.14.patch", + "@types/sinonjs__fake-timers@8.1.5": "patches/@types__sinonjs__fake-timers@8.1.5.patch" } }, "simple-git-hooks": { diff --git a/patches/@types__sinonjs__fake-timers@8.1.5.patch b/patches/@types__sinonjs__fake-timers@8.1.5.patch new file mode 100644 index 000000000000..4f52bfa71f59 --- /dev/null +++ b/patches/@types__sinonjs__fake-timers@8.1.5.patch @@ -0,0 +1,34 @@ +diff --git a/index.d.ts b/index.d.ts +index 5aa018cde4336aca4dadefb8338549c378792e14..2e0c38efc15e793dc37e401e2513016247058f37 100644 +--- a/index.d.ts ++++ b/index.d.ts +@@ -329,13 +329,15 @@ export interface FakeTimerInstallOpts { + now?: number | Date | undefined; + + /** +- * An array with names of global methods and APIs to fake. By default, `@sinonjs/fake-timers` does not replace `nextTick()` and `queueMicrotask()`. +- * For instance, `FakeTimers.install({ toFake: ['setTimeout', 'nextTick'] })` will fake only `setTimeout()` and `nextTick()` ++ * An array with names of global methods and APIs to fake. ++ * For instance, `vi.useFakeTimer({ toFake: ['setTimeout', 'performance'] })` will fake only `setTimeout()` and `performance.now()` ++ * @default ['setTimeout', 'clearTimeout', 'setImmediate', 'clearImmediate', 'setInterval', 'clearInterval', 'Date'] + */ + toFake?: FakeMethod[] | undefined; + + /** +- * The maximum number of timers that will be run when calling runAll() (default: 1000) ++ * The maximum number of timers that will be run when calling runAll() ++ * @default 10000 + */ + loopLimit?: number | undefined; + +@@ -352,8 +354,8 @@ export interface FakeTimerInstallOpts { + advanceTimeDelta?: number | undefined; + + /** +- * Tells FakeTimers to clear 'native' (i.e. not fake) timers by delegating to their respective handlers. These are not cleared by +- * default, leading to potentially unexpected behavior if timers existed prior to installing FakeTimers. (default: false) ++ * Tells FakeTimers to clear 'native' (i.e. not fake) timers by delegating to their respective handlers. ++ * @default true + */ + shouldClearNativeTimers?: boolean | undefined; + } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dab06b605830..c8c29373dd2e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,9 @@ patchedDependencies: '@types/chai@4.3.6': hash: s5kzatt2y2dzfxfynxzvzt5kbm path: patches/@types__chai@4.3.6.patch + '@types/sinonjs__fake-timers@8.1.5': + hash: ggdsr7nrdrzokhhihsihc2hdja + path: patches/@types__sinonjs__fake-timers@8.1.5.patch cac@6.7.14: hash: slh3cigivjjjktoa42g2agwaem path: patches/cac@6.7.14.patch @@ -1393,7 +1396,7 @@ importers: version: 2.4.9 '@types/sinonjs__fake-timers': specifier: ^8.1.5 - version: 8.1.5 + version: 8.1.5(patch_hash=ggdsr7nrdrzokhhihsihc2hdja) birpc: specifier: 0.2.15 version: 0.2.15 @@ -9611,9 +9614,10 @@ packages: resolution: {integrity: sha512-0kSuKjAS0TrGLJ0M/+8MaFkGsQhZpB6pxOmvS3K8FYI72K//YmdfoW9X2qPsAKh1mkwxGD5zib9s1FIFed6E8g==} dev: true - /@types/sinonjs__fake-timers@8.1.5: + /@types/sinonjs__fake-timers@8.1.5(patch_hash=ggdsr7nrdrzokhhihsihc2hdja): resolution: {integrity: sha512-mQkU2jY8jJEF7YHjHvsQO8+3ughTL1mcnn96igfhONmR+fUPSKIkefQYpSe8bsly2Ep7oQbn/6VG5/9/0qcArQ==} dev: true + patched: true /@types/sizzle@2.3.3: resolution: {integrity: sha512-JYM8x9EGF163bEyhdJBpR2QX1R5naCJHC8ucJylJ3w9/CVBaskdQ8WqBf8MmQrd1kRvp/a4TS8HJ+bxzR7ZJYQ==} From f65a4744c25c994773475c5d25e5f7309aa2ce00 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 18 Feb 2024 09:57:18 +0900 Subject: [PATCH 3/3] docs: Update docs/api/vi.md Co-authored-by: Anjorin Damilare --- docs/api/vi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/vi.md b/docs/api/vi.md index eb2d419fac65..a10e3e0b222f 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -738,7 +738,7 @@ vi.useRealTimers() - **Type:** `() => Vitest` -To enable mocking timers, you need to call this method. It will wrap all further calls to timers (such as `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `setImmediate`, `clearImmediate`, and `Date`), until [`vi.useRealTimers()`](#vi-userealtimers) is called. +To enable mocking timers, you need to call this method. It will wrap all further calls to timers (such as `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `setImmediate`, `clearImmediate`, and `Date`) until [`vi.useRealTimers()`](#vi-userealtimers) is called. Mocking `nextTick` is not supported when running Vitest inside `node:child_process` by using `--pool=forks`. NodeJS uses `process.nextTick` internally in `node:child_process` and hangs when it is mocked. Mocking `nextTick` is supported when running Vitest with `--pool=threads`.