Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Jest compatible `expect` assertion functions
Functions
Interfaces
The Expected interface defines the available assertion methods.
- lastCalledWith(...expected: unknown[]): void
Asserts that the function was called with the specified arguments.
- lastReturnedWith(expected: unknown): void
Asserts that the function returned the specified value.
- not: IsAsync extends true ? Async<Expected<true>> : Expected<false>
The negation object that allows chaining negated assertions.
- nthCalledWith(): voidnth: number,...expected: unknown[]
Asserts that the function was called with the specified arguments at the specified call index.
- nthReturnedWith(): voidnth: number,expected: unknown
Asserts that the function returned the specified value at the specified call index.
- rejects: Async<Expected<true>>
The object that allows chaining assertions with async functions that are expected to throw an error.
- resolves: Async<Expected<true>>
The object that allows chaining assertions with async functions that are expected to resolve to a value.
- toBe(expected: unknown): void
Asserts that the value is equal to the specified value.
- toBeCalled(): void
Asserts that the function was called at least once.
- toBeCalledTimes(expected: number): void
Asserts that the function was called the specified number of times.
- toBeCalledWith(...expected: unknown[]): void
Asserts that the function was called with the specified arguments.
- toBeCloseTo(): voidcandidate: number,tolerance?: number
Asserts that a given numerical value is approximately equal to an expected number within a certain margin of error (tolerance). Useful when comparing floating-point numbers, which may be represented internally with precision errors.
- toBeDefined(): void
Asserts that the value is defined.
- toBeFalsy(): void
Asserts that the value is falsy.
- toBeGreaterThan(expected: number): void
Asserts that the value is greater than the specified number.
- toBeGreaterThanOrEqual(expected: number): void
Asserts that the value is greater than or equal to the specified number.
- toBeInstanceOf<T extends AnyConstructor>(expected: T): void
Asserts that the value is an instance of the specified constructor.
- toBeLessThan(expected: number): void
Asserts that the value is less than the specified number.
- toBeLessThanOrEqual(expected: number): void
Asserts that the value is less than or equal to the specified number.
- toBeNaN(): void
Asserts that the value is NaN.
- toBeNull(): void
Asserts that the value is null.
- toBeTruthy(): void
Asserts that the value is truthy.
- toBeUndefined(): void
Asserts that the value is undefined.
- toContain(expected: unknown): void
Asserts that the value contains the specified value (shallow equality).
- toContainEqual(expected: unknown): void
Asserts that the value contains the specified value (deep equality).
- toEqual(expected: unknown): void
Asserts that the value is equal to the specified value (deep equality).
- toHaveBeenCalled(): void
Asserts that the function was called at least once.
- toHaveBeenCalledTimes(expected: number): void
Asserts that the function was called the specified number of times.
- toHaveBeenCalledWith(...expected: unknown[]): void
Asserts that the function was called with the specified arguments.
- toHaveBeenLastCalledWith(...expected: unknown[]): void
Asserts that the function was last called with the specified arguments.
- toHaveBeenNthCalledWith(): voidnth: number,...expected: unknown[]
Asserts that the function was called with the specified arguments at the specified call index.
- toHaveLastReturnedWith(expected: unknown): void
Asserts that the function last returned the specified value.
- toHaveLength(expected: number): void
Asserts that the value has the specified length.
- toHaveNthReturnedWith(): voidnth: number,expected: unknown
Asserts that the function returned the specified value at the specified call index.
- toHaveProperty(): voidpropName: string | string[],value?: unknown
Asserts that the value has the specified property with an optional value.
- toHaveReturned(): void
Asserts that the function returned.
- toHaveReturnedTimes(expected: number): void
Asserts that the function returned the specified number of times.
- toHaveReturnedWith(expected: unknown): void
Asserts that the function returned the specified value.
- toMatch(expected: RegExp): void
Asserts that the value matches the specified regular expression.
- toMatchInlineSnapshot(inlineSnapshot?: string): void
Asserts that the value matches the inline snapshot string. If no snapshot is provided or the value differs, the source file will be updated when running in update mode (
-- --update). - toMatchObject(expected:Record<PropertyKey, unknown>): void
| Record<PropertyKey, unknown>[]Asserts that the value matches the specified object (deep equality).
- toMatchSnapshot(hint?: string): void
Asserts that the value matches the most recent snapshot. If no snapshot exists, one will be created when running in update mode (
-- --update). - toReturn(): void
Asserts that the function returned.
- toReturnTimes(expected: number): void
Asserts that the function returns the specified number of times.
- toReturnWith(expected: unknown): void
Asserts that the function returns the specified value.
- toStrictEqual(candidate: unknown): void
Asserts that the value is strictly equal to the specified value.
- toThrow<E extends Error = Error>(expected?: string | RegExp | E | (new (...args: any[]) => E)): void
Asserts that the function throws an error.
State for snapshot testing, following Jest's
expect.getState()/expect.setState() API.
- currentTestName: string | undefined
The name of the currently running test. Required for snapshot testing.
- testPath: string | undefined
The file path of the currently running test. Auto-detected from stack trace if not set.
Type Aliases
A constructor that accepts any args and returns any value
converts all the methods in an interface to be async functions
Variables
You can use expect.addEqualityTesters to add your own methods to test if two
objects are equal. For example, let's say you have a class in your code that
represents volume and can determine if two volumes using different units are
equal. You may want toEqual (and other equality matchers) to use this custom
equality method when comparing to Volume classes. You can add a custom equality
tester to have toEqual detect and apply custom logic when comparing Volume
classes:
expect.addSnapshotSerializer adds a module that formats application-specific data structures.
expect.any(constructor) matches anything that was created with the given
constructor or if it's a primitive that is of the passed type. You can use it
inside toEqual or toHaveBeenCalledWith instead of a literal value.
expect.anything() matches anything but null or undefined. You can use it
inside toEqual or toHaveBeenCalledWith instead of a literal value.
expect.arrayContaining(array) matches a received array which contains all of
the elements in the expected array. That is, the expected array is a subset
of the received array. Therefore, it matches a received array which contains
elements that are not in the expected array.
expect.assertions verifies that a certain number of assertions are called during a test.
expect.closeTo(number, numDigits?) is useful when comparing floating point
numbers in object properties or array item. If you need to compare a number,
please use .toBeCloseTo instead.
Extend expect() with custom provided matchers.
expect.getState returns the current state for snapshot matchers.
expect.hasAssertions verifies that at least one assertion is called during a test.
expect.not.arrayContaining matches a received array which does not contain
all of the elements in the expected array. That is, the expected array is not
a subset of the received array.
- No documentation available
- No documentation available
- No documentation available
- No documentation available
expect.objectContaining(object) matches any received object that recursively matches the expected properties.
That is, the expected object is not a subset of the received object. Therefore, it matches a received object
which contains properties that are not in the expected object.
expect.setState sets the state for snapshot matchers. This must be called
before using toMatchSnapshot() to provide the current test name and
optionally the test file path.
expect.stringContaining(string) matches the received value if it is a string
that contains the exact expected string.
expect.stringMatching(string | regexp) matches the received value if it is a
string that matches the expected string or regular expression.