Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Matchers<R, T>

Type parameters

  • R

  • T

Hierarchy

  • Matchers

Index

Methods

lastCalledWith

  • lastCalledWith<E>(...args: E): R
  • Ensures the last call to a mock function was provided specific args.

    Optionally, you can provide a type for the expected arguments via a generic. Note that the type must be either an array or a tuple.

    Type parameters

    • E: any[]

    Parameters

    • Rest ...args: E

    Returns R

lastReturnedWith

  • lastReturnedWith<E>(value: E): R
  • Ensure that the last call to a mock function has returned a specified value.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • value: E

    Returns R

nthCalledWith

  • nthCalledWith<E>(nthCall: number, ...params: E): R
  • Ensure that a mock function is called with specific arguments on an Nth call.

    Optionally, you can provide a type for the expected arguments via a generic. Note that the type must be either an array or a tuple.

    Type parameters

    • E: any[]

    Parameters

    • nthCall: number
    • Rest ...params: E

    Returns R

nthReturnedWith

  • nthReturnedWith<E>(n: number, value: E): R
  • Ensure that the nth call to a mock function has returned a specified value.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • n: number
    • value: E

    Returns R

toBe

  • toBe<E>(expected: E): R
  • Checks that a value is what you expect. It uses Object.is to check strict equality. Don't use toBe with floating-point numbers.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toBeCalled

  • toBeCalled(): R
  • Ensures that a mock function is called.

    Returns R

toBeCalledTimes

  • toBeCalledTimes(expected: number): R
  • Ensures that a mock function is called an exact number of times.

    Parameters

    • expected: number

    Returns R

toBeCalledWith

  • toBeCalledWith<E>(...args: E): R
  • Ensure that a mock function is called with specific arguments.

    Optionally, you can provide a type for the expected arguments via a generic. Note that the type must be either an array or a tuple.

    Type parameters

    • E: any[]

    Parameters

    • Rest ...args: E

    Returns R

toBeCloseTo

  • toBeCloseTo(expected: number, numDigits?: undefined | number): R
  • Using exact equality with floating point numbers is a bad idea. Rounding means that intuitive things fail. The default for numDigits is 2.

    Parameters

    • expected: number
    • Optional numDigits: undefined | number

    Returns R

toBeDefined

  • toBeDefined(): R
  • Ensure that a variable is not undefined.

    Returns R

toBeFalsy

  • toBeFalsy(): R
  • When you don't care what a value is, you just want to ensure a value is false in a boolean context.

    Returns R

toBeGreaterThan

  • toBeGreaterThan(expected: number): R
  • For comparing floating point numbers.

    Parameters

    • expected: number

    Returns R

toBeGreaterThanOrEqual

  • toBeGreaterThanOrEqual(expected: number): R
  • For comparing floating point numbers.

    Parameters

    • expected: number

    Returns R

toBeInstanceOf

  • toBeInstanceOf<E>(expected: E): R
  • Ensure that an object is an instance of a class. This matcher uses instanceof underneath.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toBeLessThan

  • toBeLessThan(expected: number): R
  • For comparing floating point numbers.

    Parameters

    • expected: number

    Returns R

toBeLessThanOrEqual

  • toBeLessThanOrEqual(expected: number): R
  • For comparing floating point numbers.

    Parameters

    • expected: number

    Returns R

toBeNaN

  • toBeNaN(): R
  • Used to check that a variable is NaN.

    Returns R

toBeNull

  • toBeNull(): R
  • This is the same as .toBe(null) but the error messages are a bit nicer. So use .toBeNull() when you want to check that something is null.

    Returns R

toBeTruthy

  • toBeTruthy(): R
  • Use when you don't care what a value is, you just want to ensure a value is true in a boolean context. In JavaScript, there are six falsy values: false, 0, '', null, undefined, and NaN. Everything else is truthy.

    Returns R

toBeUndefined

  • toBeUndefined(): R
  • Used to check that a variable is undefined.

    Returns R

toContain

  • toContain<E>(expected: E): R
  • Used when you want to check that an item is in a list. For testing the items in the list, this uses ===, a strict equality check.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toContainEqual

  • toContainEqual<E>(expected: E): R
  • Used when you want to check that an item is in a list. For testing the items in the list, this matcher recursively checks the equality of all fields, rather than checking for object identity.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toEqual

  • toEqual<E>(expected: E): R
  • Used when you want to check that two objects have the same value. This matcher recursively checks the equality of all fields, rather than checking for object identity.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toHaveBeenCalled

  • toHaveBeenCalled(): R
  • Ensures that a mock function is called.

    Returns R

toHaveBeenCalledTimes

  • toHaveBeenCalledTimes(expected: number): R
  • Ensures that a mock function is called an exact number of times.

    Parameters

    • expected: number

    Returns R

toHaveBeenCalledWith

  • toHaveBeenCalledWith<E>(...params: E): R
  • Ensure that a mock function is called with specific arguments.

    Optionally, you can provide a type for the expected arguments via a generic. Note that the type must be either an array or a tuple.

    Type parameters

    • E: any[]

    Parameters

    • Rest ...params: E

    Returns R

toHaveBeenLastCalledWith

  • toHaveBeenLastCalledWith<E>(...params: E): R
  • If you have a mock function, you can use .toHaveBeenLastCalledWith to test what arguments it was last called with.

    Optionally, you can provide a type for the expected arguments via a generic. Note that the type must be either an array or a tuple.

    Type parameters

    • E: any[]

    Parameters

    • Rest ...params: E

    Returns R

toHaveBeenNthCalledWith

  • toHaveBeenNthCalledWith<E>(nthCall: number, ...params: E): R
  • Ensure that a mock function is called with specific arguments on an Nth call.

    Optionally, you can provide a type for the expected arguments via a generic. Note that the type must be either an array or a tuple.

    Type parameters

    • E: any[]

    Parameters

    • nthCall: number
    • Rest ...params: E

    Returns R

toHaveLastReturnedWith

  • toHaveLastReturnedWith<E>(expected: E): R
  • Use to test the specific value that a mock function last returned. If the last call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toHaveLength

  • toHaveLength(expected: number): R
  • Used to check that an object has a .length property and it is set to a certain numeric value.

    Parameters

    • expected: number

    Returns R

toHaveNthReturnedWith

  • toHaveNthReturnedWith<E>(nthCall: number, expected: E): R
  • Use to test the specific value that a mock function returned for the nth call. If the nth call to the mock function threw an error, then this matcher will fail no matter what value you provided as the expected return value.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • nthCall: number
    • expected: E

    Returns R

toHaveProperty

  • toHaveProperty<E>(propertyPath: string | any[], value?: E): R
  • Use to check if property at provided reference keyPath exists for an object. For checking deeply nested properties in an object you may use dot notation or an array containing the keyPath for deep references.

    Optionally, you can provide a value to check if it's equal to the value present at keyPath on the target object. This matcher uses 'deep equality' (like toEqual()) and recursively checks the equality of all fields.

    example

    expect(houseForSale).toHaveProperty('kitchen.area', 20);

    Type parameters

    • E

    Parameters

    • propertyPath: string | any[]
    • Optional value: E

    Returns R

toHaveReturned

  • toHaveReturned(): R
  • Use to test that the mock function successfully returned (i.e., did not throw an error) at least one time

    Returns R

toHaveReturnedTimes

  • toHaveReturnedTimes(expected: number): R
  • Use to ensure that a mock function returned successfully (i.e., did not throw an error) an exact number of times. Any calls to the mock function that throw an error are not counted toward the number of times the function returned.

    Parameters

    • expected: number

    Returns R

toHaveReturnedWith

  • toHaveReturnedWith<E>(expected: E): R
  • Use to ensure that a mock function returned a specific value.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toMatch

  • toMatch(expected: string | RegExp): R
  • Check that a string matches a regular expression.

    Parameters

    Returns R

toMatchInlineSnapshot

  • toMatchInlineSnapshot<U>(propertyMatchers: Partial<U>, snapshot?: undefined | string): R
  • toMatchInlineSnapshot(snapshot?: undefined | string): R
  • This ensures that a value matches the most recent snapshot with property matchers. Instead of writing the snapshot value to a .snap file, it will be written into the source code automatically. Check out the Snapshot Testing guide for more information.

    Type parameters

    • U: {}

    Parameters

    • propertyMatchers: Partial<U>
    • Optional snapshot: undefined | string

    Returns R

  • This ensures that a value matches the most recent snapshot with property matchers. Instead of writing the snapshot value to a .snap file, it will be written into the source code automatically. Check out the Snapshot Testing guide for more information.

    Parameters

    • Optional snapshot: undefined | string

    Returns R

toMatchObject

  • toMatchObject<E>(expected: E): R
  • Used to check that a JavaScript object matches a subset of the properties of an object

    Optionally, you can provide an object to use as Generic type for the expected value. This ensures that the matching object matches the structure of the provided object-like type.

    example

    type House = { bath: boolean; bedrooms: number; kitchen: { amenities: string[]; area: number; wallColor: string; } };

    expect(desiredHouse).toMatchObject(...standardHouse, kitchen: {area: 20}) // wherein standardHouse is some base object of type House

    Type parameters

    • E: {} | any[]

    Parameters

    • expected: E

    Returns R

toMatchSnapshot

  • toMatchSnapshot<U>(propertyMatchers: Partial<U>, snapshotName?: undefined | string): R
  • toMatchSnapshot(snapshotName?: undefined | string): R
  • This ensures that a value matches the most recent snapshot with property matchers. Check out the Snapshot Testing guide for more information.

    Type parameters

    • U: {}

    Parameters

    • propertyMatchers: Partial<U>
    • Optional snapshotName: undefined | string

    Returns R

  • This ensures that a value matches the most recent snapshot. Check out the Snapshot Testing guide for more information.

    Parameters

    • Optional snapshotName: undefined | string

    Returns R

toReturn

  • toReturn(): R
  • Ensure that a mock function has returned (as opposed to thrown) at least once.

    Returns R

toReturnTimes

  • toReturnTimes(count: number): R
  • Ensure that a mock function has returned (as opposed to thrown) a specified number of times.

    Parameters

    • count: number

    Returns R

toReturnWith

  • toReturnWith<E>(value: E): R
  • Ensure that a mock function has returned a specified value at least once.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • value: E

    Returns R

toStrictEqual

  • toStrictEqual<E>(expected: E): R
  • Use to test that objects have the same types as well as structure.

    Optionally, you can provide a type for the expected value via a generic. This is particuarly useful for ensuring expected objects have the right structure.

    Type parameters

    • E

    Parameters

    • expected: E

    Returns R

toThrow

  • Used to test that a function throws when it is called.

    Parameters

    Returns R

toThrowError

  • If you want to test that a specific error is thrown inside a function.

    Parameters

    Returns R

toThrowErrorMatchingInlineSnapshot

  • toThrowErrorMatchingInlineSnapshot(snapshot?: undefined | string): R
  • Used to test that a function throws a error matching the most recent snapshot when it is called. Instead of writing the snapshot value to a .snap file, it will be written into the source code automatically.

    Parameters

    • Optional snapshot: undefined | string

    Returns R

toThrowErrorMatchingSnapshot

  • toThrowErrorMatchingSnapshot(): R
  • Used to test that a function throws a error matching the most recent snapshot when it is called.

    Returns R