variable
test.vi
Vitest-compatible mocking utilities, for migrating tests from Vitest to Bun.
Advance the fake clock and run the timers that come due.
Like advanceTimersByTime, but runs promise jobs and one turn of the real event loop between timers, so it picks up timers that a callback schedules after an await.
Advance the fake clock to the next multiple of 16 milliseconds. That's when a faked requestAnimationFrame runs its callbacks.
Advance the fake clock to the next timer and run every timer due at that time. Repeats steps times.
Like advanceTimersToNextTimer, but runs promise jobs and one turn of the real event loop between timers.
Clear all mock state (calls, results, etc.) without restoring original implementation
Like vi.mock, but the call isn't hoisted, so it only applies to imports that run after it.
Disposing the return value removes the mock, as in using _ = vi.doMock("./math", factory).
Like vi.unmock, but the call isn't hoisted.
Wait until every in-flight import() has settled, including ones that start during the wait.
Returns the mocked time, or null if neither vi.setSystemTime() nor vi.useFakeTimers() is in effect.
Returns the real time in milliseconds since the epoch, no matter what's faked.
Calls factory and returns its result. The statement is hoisted above the file's imports, so a vi.mock factory can use the result.
const mocks = vi.hoisted(() => ({ add: vi.fn() }));
vi.mock("./math", () => ({ add: mocks.add }));Import the real module id, even if it's mocked.
Import the mocked version of the module id, even if it isn't mocked.
Like vi.mock(id, factory), but takes import("./math") instead of a string, so importOriginal is typed as that module.
Replace a module with the return value of factory. The call is hoisted above the file's imports.
Without a factory, the module is replaced with the file of the same name in a __mocks__ directory. If there isn't one, it's replaced with a copy of the module where every function is a mock. With { spy: true }, the functions keep their implementation.
import { add } from "./math";
vi.mock("./math", async importOriginal => ({ ...(await importOriginal()), add: vi.fn(() => 3) }));Deeply mock value, the same way vi.mock() mocks a module when it has no factory. Returns a copy where functions and classes are mocks that return undefined and arrays are empty. Primitives and builtin objects are kept.
With { spy: true }, functions keep their implementation, arrays keep their elements, and objects are modified in place instead of copied.
const mocked = vi.mockObject({ add: (a: number, b: number) => a + b });
mocked.add(1, 2); // undefined
mocked.add.mockReturnValue(3);Clear all mock state and implementations. A vi.fn(implementation) mock goes back to implementation, and a vi.spyOn() spy goes back to the original. Mocks made with jest.fn(), mock() or spyOn() return undefined.
Undo every vi.setConfig() call.
Re-evaluate each module on its next import() or require(). Mocked modules stay mocked.
Restore every spied-on property. A vi.spyOn() spy keeps its recorded calls and its implementation.
Run the callbacks queued with a faked process.nextTick or queueMicrotask.
Run timers until none are left. Throws after loopLimit timers.
Like runAllTimers, but runs promise jobs and one turn of the real event loop between timers.
Advance the fake clock to the last timer that's currently pending.
Like runOnlyPendingTimers, but runs promise jobs and one turn of the real event loop between timers.
Change the configuration for the rest of the test file. If a preload script calls it, the change applies to every test file. Vitest's other options are accepted but ignored.
Set the time Date reports. Without fake timers, only Date is mocked, until you call vi.useRealTimers().
Choose how the fake clock moves when the test doesn't move it.
manual: it doesn't move. This is the default.nextTimerAsync: each turn of the real event loop runs the next timer.interval: it advances byintervalmilliseconds (default 20) everyintervalreal milliseconds.
Set process.env[name], which is also import.meta.env[name]. The stub lasts until vi.unstubAllEnvs() or the end of the test file. Passing undefined deletes the variable.
Define globalThis[name]. The stub lasts until vi.unstubAllGlobals() or the end of the test file.
Stop mocking the module. The call is hoisted above the file's imports.
Restore every variable changed by vi.stubEnv() to the value it had before it was first stubbed.
Restore every global changed by vi.stubGlobal() to the property descriptor it had before it was first stubbed.
Replace the timer functions on globalThis with fakes that use a fake clock. The clock only moves when the test moves it. A reference saved before this call, such as const savedSetTimeout = setTimeout, stays real.
The real timers are restored when the test file ends, unless a preload made this call at its top level.
Restore the real timer functions and clocks, and discard pending fake timers.
Call callback right away, then every interval milliseconds, until it doesn't throw or reject. Resolves with its value. After timeout milliseconds, rejects with its last error.
The wait uses the real clock. While fake timers are on, vi.advanceTimersByTime(interval) runs before each call.
the timeout, or { timeout = 1000, interval = 50 }
await vi.waitFor(() => expect(server.isReady).toBe(true), { timeout: 500 });Call callback right away, then every interval milliseconds, until it returns a truthy value. Resolves with that value. Rejects right away if callback throws or rejects, and with a timeout error after timeout milliseconds.
The wait uses the real clock. While fake timers are on, vi.advanceTimersByTime(interval) runs before each call.
the timeout, or { timeout = 1000, interval = 50 }
const element = await vi.waitUntil(() => document.querySelector(".element"));