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

interface Expect {
    not: InverseAsymmetricMatchers;
    addSnapshotSerializer(serializer: Plugin_2): void;
    any(classType: any): any;
    anything(): any;
    arrayContaining<E = any>(arr: readonly E[]): any;
    assertions(num: number): void;
    closeTo(num: number, numDigits?: number): any;
    extend(obj: ExpectExtendMap): void;
    getState(): MatcherState & Record<string, any>;
    hasAssertions(): void;
    objectContaining<E = {}>(obj: E): any;
    setState(state: object): void;
    stringContaining(str: string): any;
    stringMatching(str: string | RegExp): any;
    <T = any>(actual: T): JestMatchers<T>;
}
  • The expect function is used every time you want to test a value. You will rarely call expect by itself.

    Type Parameters

    • T = any

    Parameters

    • actual: T

      The value to apply matchers against.

    Returns JestMatchers<T>

Properties

Methods

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

    Parameters

    • serializer: Plugin_2

    Returns void

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

    Parameters

    • classType: any

    Returns any

    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));
    });
  • 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:

    Returns any

    test('map calls its argument with a non-null argument', () => {
    const mock = jest.fn();
    [1].map(x => mock(x));
    expect(mock).toBeCalledWith(expect.anything());
    });
  • 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 = any

    Parameters

    • arr: readonly E[]

    Returns any

  • 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

  • Useful when comparing floating point numbers in object properties or array item. If you need to compare a number, use .toBeCloseTo instead.

    The optional numDigits argument limits the number of digits to check after the decimal point. For the default value 2, the test criterion is Math.abs(expected - received) < 0.005 (that is, 10 ** -2 / 2).

    Parameters

    • num: number
    • OptionalnumDigits: number

    Returns any

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

    Parameters

    Returns void

  • Returns MatcherState & Record<string, any>

  • 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

  • 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

  • Parameters

    • state: object

    Returns void

  • Matches any received string that contains the exact expected string

    Parameters

    • str: string

    Returns any

  • Matches any string that contains the exact provided string

    Parameters

    • str: string | RegExp

    Returns any