Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Expect

The expect function is used every time you want to test a value. You will rarely call expect by itself.

Hierarchy

  • Expect

Callable

  • The expect function is used every time you want to test a value. You will rarely call expect by itself.

    Type parameters

    • T

    Parameters

    • actual: T

      The value to apply matchers against.

    Returns JestMatchers<T>

Index

Properties

not

Methods

addSnapshotSerializer

  • Adds a module to format application-specific data structures for serialization.

    Parameters

    Returns void

any

  • any(classType: any): any
  • Matches anything that was created with the given constructor. You can use it inside toEqual or toBeCalledWith instead of a literal value.

    example

    function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); }

    test('randocall calls its callback with a number', () => { const mock = jest.fn(); randocall(mock); expect(mock).toBeCalledWith(expect.any(Number)); });

    Parameters

    • classType: any

    Returns any

anything

  • anything(): any
  • Matches anything but null or undefined. You can use it inside toEqual or toBeCalledWith instead of a literal value. For example, if you want to check that a mock function is called with a non-null argument:

    example

    test('map calls its argument with a non-null argument', () => { const mock = jest.fn(); [1].map(x => mock(x)); expect(mock).toBeCalledWith(expect.anything()); });

    Returns any

arrayContaining

  • arrayContaining<E>(arr: E[]): any
  • Matches any array made up entirely of elements in the provided array. You can use it inside toEqual or toBeCalledWith instead of a literal value.

    Optionally, you can provide a type for the elements via a generic.

    Type parameters

    • E

    Parameters

    • arr: E[]

    Returns any

assertions

  • assertions(num: number): void
  • Verifies that a certain number of assertions are called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called.

    Parameters

    • num: number

    Returns void

extend

  • You can use expect.extend to add your own matchers to Jest.

    Parameters

    Returns void

getState

  • Returns MatcherState & Record<string, any>

hasAssertions

  • hasAssertions(): void
  • Verifies that at least one assertion is called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called.

    Returns void

objectContaining

  • objectContaining<E>(obj: E): any
  • Matches any object that recursively matches the provided keys. This is often handy in conjunction with other asymmetric matchers.

    Optionally, you can provide a type for the object via a generic. This ensures that the object contains the desired structure.

    Type parameters

    • E

    Parameters

    • obj: E

    Returns any

setState

  • setState(state: object): void
  • Parameters

    • state: object

    Returns void

stringContaining

  • stringContaining(str: string): any
  • Matches any received string that contains the exact expected string

    Parameters

    • str: string

    Returns any

stringMatching

  • stringMatching(str: string | RegExp): any
  • Matches any string that contains the exact provided string

    Parameters

    Returns any