namespace

test.jest

namespace jest

  • type Mock<T extends (...args: any[]) => any = (...args: any[]) => any> = JestMock.Mock<T>

    Constructs the type of a mock function, such as the return type of jest.fn().

  • type Replaced<T> = JestMock.Replaced<T>

    Constructs the type of a replaced property.

  • type Spied<T extends JestMock.ClassLike | (...args: any[]) => any> = JestMock.Spied<T>

    Constructs the type of a spied class or function.

  • type SpiedClass<T extends JestMock.ClassLike> = JestMock.SpiedClass<T>

    Constructs the type of a spied class.

  • type SpiedFunction<T extends (...args: any[]) => any> = JestMock.SpiedFunction<T>

    Constructs the type of a spied function.

  • type SpiedGetter<T> = JestMock.SpiedGetter<T>

    Constructs the type of a spied getter.

  • type SpiedSetter<T> = JestMock.SpiedSetter<T>

    Constructs the type of a spied setter.

  • milliseconds: number
    ): typeof jest;

    Advance the fake clock and run the timers that come due.

  • milliseconds: number
    ): Promise<void>;

    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.

  • function advanceTimersToNextFrame(): typeof jest;

    Advance the fake clock to the next multiple of 16 milliseconds. That's when a faked requestAnimationFrame runs its callbacks.

  • steps?: number
    ): typeof jest;

    Advance the fake clock to the next timer and run every timer due at that time. Repeats steps times.

  • steps?: number
    ): Promise<void>;

    Like advanceTimersToNextTimer, but runs promise jobs and one turn of the real event loop between timers.

  • function clearAllMocks(): typeof jest;
  • function clearAllTimers(): void;
  • function createMockFromModule<T = any>(
    id: string
    ): Mocked<T>;

    require() the real module id and return a copy where every function is a mock.

  • function doMock(
    id: string,
    factory?: ModuleMockFactory<any>
    ): typeof jest;

    Like mock, but the call isn't hoisted.

  • function dontMock(
    id: string
    ): typeof jest;

    Like unmock, but the call isn't hoisted.

  • function fn<T extends (...args: any[]) => any>(
    func?: T
    ): Mock<T>;
    function fn<Args extends any[], Instance>(
    func: new (...args: Args) => Instance
    ): Mock<(...args: Args) => Instance>;
  • function getRealSystemTime(): number;

    Returns the real time in milliseconds since the epoch, no matter what's faked.

  • function getTimerCount(): number;
  • function isFakeTimers(): boolean;
  • function isMockFunction(
    fn: unknown
    ): fn is Mock<(...args: any[]) => any>;

    Returns true if fn is a mock function or a spy.

  • function mock(
    id: string,
    factory?: ModuleMockFactory<any>
    ): typeof jest;

    Replace the module id 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.

  • function mocked<T>(
    item: T,
    options?: unknown
    ): Mocked<T>;

    Returns item typed as Mocked. Does nothing at runtime.

  • function requireActual<T = any>(
    id: string
    ): T;

    require() the real module id, even if it's mocked.

  • function requireMock<T = any>(
    id: string
    ): T;

    require() the mocked version of the module id, even if it isn't mocked.

  • function resetAllMocks(): typeof jest;
  • function resetModules(): typeof jest;

    Re-evaluate each module on its next import() or require(). Mocked modules stay mocked, and their factories run again.

  • function restoreAllMocks(): typeof jest;
  • function runAllImmediates(): typeof jest;

    Run the callbacks queued with a faked setImmediate.

  • function runAllTicks(): typeof jest;

    Run the callbacks queued with a faked process.nextTick or queueMicrotask.

  • function runAllTimers(): typeof jest;

    Run timers until none are left. Throws after timerLimit timers.

  • function runAllTimersAsync(): Promise<void>;

    Like runAllTimers, but runs promise jobs and one turn of the real event loop between timers.

  • function runOnlyPendingTimers(): typeof jest;

    Advance the fake clock to the last timer that's currently pending.

  • function runOnlyPendingTimersAsync(): Promise<void>;

    Like runOnlyPendingTimers, but runs promise jobs and one turn of the real event loop between timers.

  • function setSystemTime(
    now?: string | number | Date
    ): void;
  • function setTimeout(
    milliseconds: number
    ): void;
  • config: { mode: 'manual' | 'nextAsync' } | { delta: number; mode: 'interval' }
    ): typeof jest;

    Choose how the fake clock moves when the test doesn't move it.

    • manual: it doesn't move. This is the default.
    • nextAsync: each turn of the real event loop runs the next timer.
    • interval: it advances by delta milliseconds (default 20) every delta real milliseconds.
  • function spyOn<T extends object, K extends string | number | symbol>(
    obj: T,
    property: K,
    accessType: 'get'
    ): Mock<() => T[K]>;
    function spyOn<T extends object, K extends string | number | symbol>(
    obj: T,
    property: K,
    accessType: 'set'
    ): Mock<(value: T[K]) => void>;
    function spyOn<T extends object, K extends string | number | symbol>(
    obj: T,
    methodOrPropertyValue: K
    ): Mock<Extract<T[K], (...args: any[]) => any>>;
  • function unmock(
    id: string
    ): typeof jest;

    Stop mocking the module id. The call is hoisted above the file's imports.

  • function useFakeTimers(
    options?: 'modern' | FakeTimersOptions | 'legacy'
    ): typeof jest;

    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.

  • function useRealTimers(): typeof jest;

    Restore the real timer functions and clocks, and discard pending fake timers.