Skip to content

Commit

Permalink
docs: add default for fakeTimers.toFake (#5197)
Browse files Browse the repository at this point in the history
Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
  • Loading branch information
3 people committed Mar 26, 2024
1 parent fee7d8b commit 2e8e05e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/api/vi.md
Expand Up @@ -738,7 +738,7 @@ vi.useRealTimers()

- **Type:** `(config?: FakeTimerInstallOpts) => 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`.

Expand Down
5 changes: 3 additions & 2 deletions docs/config/index.md
Expand Up @@ -2046,9 +2046,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']`.

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -83,7 +83,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": {
Expand Down
34 changes: 34 additions & 0 deletions 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;
}
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit 2e8e05e

Please sign in to comment.