Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • Jest

Index

Properties

fn

Creates a mock function. Optionally takes a mock implementation.

requireActual

requireActual: function

Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not.

Type declaration

    • (moduleName: string): unknown
    • Parameters

      • moduleName: string

      Returns unknown

requireMock

requireMock: function

Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not.

Type declaration

    • (moduleName: string): unknown
    • Parameters

      • moduleName: string

      Returns unknown

spyOn

Creates a mock function similar to jest.fn but also tracks calls to object[methodName].

Note: By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries.

Methods

addMatchers

  • addMatchers(matchers: Record<string, any>): void
  • Provides a way to add Jasmine-compatible matchers into your Jest context.

    deprecated

    Use expect.extend instead

    Parameters

    • matchers: Record<string, any>

    Returns void

advanceTimersByTime

  • advanceTimersByTime(msToRun: number): void
  • Advances all timers by msToRun milliseconds. All pending "macro-tasks" that have been queued via setTimeout() or setInterval(), and would be executed within this timeframe will be executed.

    Parameters

    • msToRun: number

    Returns void

autoMockOff

  • autoMockOff(): Jest
  • Disables automatic mocking in the module loader.

    Returns Jest

autoMockOn

  • autoMockOn(): Jest
  • Enables automatic mocking in the module loader.

    Returns Jest

clearAllMocks

  • clearAllMocks(): Jest
  • Clears the mock.calls and mock.instances properties of all mocks. Equivalent to calling .mockClear() on every mocked function.

    Returns Jest

clearAllTimers

  • clearAllTimers(): void
  • Removes any pending timers from the timer system. If any timers have been scheduled, they will be cleared and will never have the opportunity to execute in the future.

    Returns void

deepUnmock

  • deepUnmock(moduleName: string): Jest
  • Indicates that the module system should never return a mocked version of the specified module, including all of the specified module's dependencies.

    Parameters

    • moduleName: string

    Returns Jest

disableAutomock

  • disableAutomock(): Jest
  • Disables automatic mocking in the module loader.

    After this method is called, all require()s will return the real versions of each module (rather than a mocked version).

    Returns Jest

doMock

  • doMock(moduleName: string, moduleFactory?: function): Jest
  • When using babel-jest, calls to mock will automatically be hoisted to the top of the code block. Use this method if you want to explicitly avoid this behavior.

    Parameters

    • moduleName: string
    • Optional moduleFactory: function
        • (): unknown
        • Returns unknown

    Returns Jest

dontMock

  • dontMock(moduleName: string): Jest
  • Indicates that the module system should never return a mocked version of the specified module from require() (e.g. that it should always return the real module).

    Parameters

    • moduleName: string

    Returns Jest

enableAutomock

  • enableAutomock(): Jest
  • Enables automatic mocking in the module loader.

    Returns Jest

genMockFromModule

  • genMockFromModule(moduleName: string): unknown
  • Given the name of a module, use the automatic mocking system to generate a mocked version of the module for you.

    This is useful when you want to create a manual mock that extends the automatic mock's behavior.

    Parameters

    • moduleName: string

    Returns unknown

getTimerCount

  • getTimerCount(): number
  • Returns the number of fake timers still left to run.

    Returns number

isMockFunction

  • isMockFunction(fn: Function): boolean
  • Determines if the given function is a mocked function.

    Parameters

    • fn: Function

    Returns boolean

isolateModules

  • isolateModules(fn: function): Jest
  • jest.isolateModules(fn) goes a step further than jest.resetModules() and creates a sandbox registry for the modules that are loaded inside the callback function. This is useful to isolate specific modules for every test so that local module state doesn't conflict between tests.

    Parameters

    • fn: function
        • (): void
        • Returns void

    Returns Jest

mock

  • mock(moduleName: string, moduleFactory?: function, options?: object): Jest
  • Mocks a module with an auto-mocked version when it is being required.

    Parameters

    • moduleName: string
    • Optional moduleFactory: function
        • (): unknown
        • Returns unknown

    • Optional options: object
      • Optional virtual?: boolean

    Returns Jest

resetAllMocks

  • resetAllMocks(): Jest
  • Resets the state of all mocks. Equivalent to calling .mockReset() on every mocked function.

    Returns Jest

resetModuleRegistry

  • resetModuleRegistry(): Jest
  • Resets the module registry - the cache of all required modules. This is useful to isolate modules where local state might conflict between tests.

    deprecated

    Use jest.resetModules()

    Returns Jest

resetModules

  • resetModules(): Jest
  • Resets the module registry - the cache of all required modules. This is useful to isolate modules where local state might conflict between tests.

    Returns Jest

restoreAllMocks

  • restoreAllMocks(): Jest
  • Restores all mocks back to their original value. Equivalent to calling .mockRestore on every mocked function.

    Beware that jest.restoreAllMocks() only works when mock was created with jest.spyOn; other mocks will require you to manually restore them.

    Returns Jest

retryTimes

  • retryTimes(numRetries: number): Jest
  • Runs failed tests n-times until they pass or until the max number of retries is exhausted. This only works with jest-circus!

    Parameters

    • numRetries: number

    Returns Jest

runAllImmediates

  • runAllImmediates(): void
  • Exhausts tasks queued by setImmediate().

    Returns void

runAllTicks

  • runAllTicks(): void
  • Exhausts the micro-task queue (usually interfaced in node via process.nextTick).

    Returns void

runAllTimers

  • runAllTimers(): void
  • Exhausts the macro-task queue (i.e., all tasks queued by setTimeout() and setInterval()).

    Returns void

runOnlyPendingTimers

  • runOnlyPendingTimers(): void
  • Executes only the macro-tasks that are currently pending (i.e., only the tasks that have been queued by setTimeout() or setInterval() up to this point). If any of the currently pending macro-tasks schedule new macro-tasks, those new tasks will not be executed by this call.

    Returns void

runTimersToTime

  • runTimersToTime(msToRun: number): void
  • Executes only the macro task queue (i.e. all tasks queued by setTimeout() or setInterval() and setImmediate()).

    deprecated

    Use jest.advanceTimersByTime()

    Parameters

    • msToRun: number

    Returns void

setMock

  • setMock(moduleName: string, moduleExports: unknown): Jest
  • Explicitly supplies the mock object that the module system should return for the specified module.

    Note It is recommended to use jest.mock() instead. The jest.mock API's second argument is a module factory instead of the expected exported module object.

    Parameters

    • moduleName: string
    • moduleExports: unknown

    Returns Jest

setTimeout

  • setTimeout(timeout: number): Jest
  • Set the default timeout interval for tests and before/after hooks in milliseconds.

    Note: The default timeout interval is 5 seconds if this method is not called.

    Parameters

    • timeout: number

    Returns Jest

unmock

  • unmock(moduleName: string): Jest
  • Indicates that the module system should never return a mocked version of the specified module from require() (e.g. that it should always return the real module).

    Parameters

    • moduleName: string

    Returns Jest

useFakeTimers

  • useFakeTimers(): Jest
  • Instructs Jest to use fake versions of the standard timer functions.

    Returns Jest

useRealTimers

  • useRealTimers(): Jest
  • Instructs Jest to use the real versions of the standard timer functions.

    Returns Jest

Generated using TypeDoc