bun:test

Bun

Module

bun:test

The 'bun:test' module is a fast, built-in test runner that aims for Jest compatibility. Tests are executed with the Bun runtime, providing significantly improved performance over traditional test runners.

Key features include TypeScript and JSX support, lifecycle hooks (beforeAll, beforeEach, afterEach, afterAll), snapshot testing, UI & DOM testing, watch mode, and script pre-loading. The API supports test assertions with expect, test grouping with describe, mocks, and more.

While Bun aims for compatibility with Jest, not everything is implemented yet. Use bun test to automatically discover and run tests matching common patterns like *.test.ts or *.spec.js.

functionafterAll(fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void): void

Runs a function, once, after all the tests.

This is useful for running clean up tasks, like closing a socket or deleting temporary files.

fn

the function to run

functionafterEach(fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void): void

Runs a function after each test.

This is useful for running clean up tasks, like closing a socket or deleting temporary files.

fn

the function to run

functionbeforeAll(fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void): void

Runs a function, once, before all the tests.

This is useful for running set up tasks, like initializing a global variable or connecting to a database.

If this function throws, tests will not run in this file.

fn

the function to run

functionbeforeEach(fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void): void

Runs a function before each test.

This is useful for running set up tasks, like initializing a global variable or connecting to a database.

If this function throws, the test will not run.

fn

the function to run

Vdescribe: Describe

Describes a group of related tests.

Vexpect: Expect

Asserts that a value matches some criteria.

Vjest: Jest

Vmock:
{

module(id: string, factory: () => any): void | Promise<void>

Replace the module id with the return value of factory.

This is useful for mocking modules.

id

module ID to mock

factory

a function returning an object that will be used as the exports of the mocked module

restore(): void

Restore the previous value of mocks.

}

functionsetDefaultTimeout(milliseconds: number): void

Sets the default timeout for all tests in the current file. If a test specifies a timeout, it will override this value. The default timeout is 5000ms (5 seconds).

milliseconds

the number of milliseconds for the default timeout

functionsetSystemTime(now?: number | Date): ThisType<void>

Control the system time used by:

  • Date.now()
  • new Date()
  • Intl.DateTimeFormat().format()

In the future, we may add support for more functions, but we haven't done that yet.

now

The time to set the system time to. If not provided, the system time will be reset.

returns

this

functionspyOn<T extends object, K extends string | number | symbol>(obj: T, methodOrPropertyValue: K): Mock<T[K] extends (args: any[]) => any ? any[any] : never>

Vtest: Test

Runs a test.

Type Definitions

typeAsymmetricMatcher=any

Object representing an asymmetric matcher obtained by an static call to expect like expect.anything(), expect.stringContaining("..."), etc.

interfaceAsymmetricMatchersextendsAsymmetricMatchersBuiltin

You can extend this interface with declaration merging, in order to add type support for custom asymmetric matchers.

any(constructor: (args: any[]) => any | new (args: any[]) => any): any

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

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:

arrayContaining<E=any>(arr: readonly 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.

closeTo(num: number, numDigits?: number): any

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).

objectContaining(obj: object): 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.

stringContaining(str: string | String): any

Matches any received string that contains the exact expected string

stringMatching(regex: string | String | RegExp): any

Matches any string that contains the exact provided string

interfaceAsymmetricMatchersBuiltin

any(constructor: (args: any[]) => any | new (args: any[]) => any): any

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

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:

arrayContaining<E=any>(arr: readonly 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.

closeTo(num: number, numDigits?: number): any

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).

objectContaining(obj: object): 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.

stringContaining(str: string | String): any

Matches any received string that contains the exact expected string

stringMatching(regex: string | String | RegExp): any

Matches any string that contains the exact provided string

typeCustomMatcher<E, P extends any[]>
={
}

typeCustomMatchersDetected=Omit<Matchers<unknown>, keyof MatchersBuiltin<unknown>> & Omit<AsymmetricMatchers, keyof AsymmetricMatchersBuiltin>

All non-builtin matchers and asymmetric matchers that have been type-registered through declaration merging

interfaceDescribe

Describes a group of related tests.

(fn: () => void) => void;

typeEqualsFunction=(a: unknown, b: unknown) => boolean

interfaceExpectextendsAsymmetricMatchers

You can extend this interface with declaration merging, in order to add type support for custom asymmetric matchers.

(actual?: T, customFailMessage?: string) => Matchers<T>;

typeExpectExtendMatchers<M>
={ [K in keyof M]: k extends keyof CustomMatchersDetected ? CustomMatcher<unknown, Parameters<CustomMatchersDetected[k]>> : CustomMatcher<unknown, any[]>}

If the types has been defined through declaration merging, enforce it. Otherwise enforce the generic custom matcher signature.

namespacejest

typeMock<T extends (args: any[]) => any=(args: any[]) => any>=JestMock.Mock<T>

Constructs the type of a mock function, e.g. the return type of jest.fn().

typeReplaced<T>=JestMock.Replaced<T>

Constructs the type of a replaced property.

typeSpied<T extends JestMock.ClassLike | (args: any[]) => any>=JestMock.Spied<T>

Constructs the type of a spied class or function.

typeSpiedClass<T extends JestMock.ClassLike>=JestMock.SpiedClass<T>

Constructs the type of a spied class.

typeSpiedFunction<T extends (args: any[]) => any>=JestMock.SpiedFunction<T>

Constructs the type of a spied function.

typeSpiedGetter<T>=JestMock.SpiedGetter<T>

Constructs the type of a spied getter.

typeSpiedSetter<T>=JestMock.SpiedSetter<T>

Constructs the type of a spied setter.

interfaceMatcherResult

message: string | () => string

pass: boolean

interfaceMatchers<T=unknown>extendsMatchersBuiltin<T>

You can extend this interface with declaration merging, in order to add type support for custom matchers.

fail: (message?: string) => void

Assertion which fails.

not: Matchers<unknown>

Negates the result of a subsequent assertion. If you know how to test something, .not lets you test its opposite.

pass: (message?: string) => void

Assertion which passes.

rejects: Matchers<unknown>

Expects the value to be a promise that rejects.

resolves: Matchers<Awaited<T>>

Expects the value to be a promise that resolves.

lastCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the nth call.

nthCalledWith(n: number, expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the nth call.

toBe(expected: T): void

Asserts that a value equals what is expected.

  • For non-primitive values, like objects and arrays, use toEqual() instead.
  • For floating-point numbers, use toBeCloseTo() instead.
expected

the expected value

toBeArray(): void

Asserts that a value is a array.

toBeArrayOfSize(size: number): void

Asserts that a value is a array of a certain length.

toBeBoolean(): void

Asserts that a value is a boolean.

toBeCalled(): void

Ensures that a mock function is called an exact number of times.

toBeCalledTimes(expected: number): void

Ensure that a mock function is called with specific arguments.

toBeCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments.

toBeCloseTo(expected: number, numDigits?: number): void

Asserts that value is close to the expected by floating point precision.

For example, the following fails because arithmetic on decimal (base 10) values often have rounding errors in limited precision binary (base 2) representation.

expected

the expected value

numDigits

the number of digits to check after the decimal point. Default is 2

toBeDate(): void

Asserts that a value is a Date object.

To check if a date is valid, use toBeValidDate() instead.

toBeDefined(): void

Asserts that a value is defined. (e.g. is not undefined)

toBeEmpty(): void

Asserts that a value is empty.

toBeEmptyObject(): void

Asserts that a value is an empty object.

toBeEven(): void

Asserts that a number is even.

toBeFalse(): void

Asserts that a value is false.

toBeFalsy(): void

Asserts that a value is "falsy".

To assert that a value equals false, use toBe(false) instead.

toBeFinite(): void

Asserts that a value is a number, and is not NaN or Infinity.

toBeFunction(): void

Asserts that a value is a function.

toBeGreaterThan(expected: number | bigint): void

Asserts that a value is a number and is greater than the expected value.

expected

the expected number

toBeGreaterThanOrEqual(expected: number | bigint): void

Asserts that a value is a number and is greater than or equal to the expected value.

expected

the expected number

toBeInstanceOf(value: unknown): void

Asserts that the expected value is an instance of value

toBeInteger(): void

Asserts that a value is a number, and is an integer.

toBeLessThan(expected: number | bigint): void

Asserts that a value is a number and is less than the expected value.

expected

the expected number

toBeLessThanOrEqual(expected: number | bigint): void

Asserts that a value is a number and is less than or equal to the expected value.

expected

the expected number

toBeNaN(): void

Asserts that a value is NaN.

Same as using Number.isNaN().

toBeNegative(): void

Asserts that a value is a negative number.

toBeNil(): void

Asserts that a value is null or undefined.

toBeNull(): void

Asserts that a value is null.

toBeNumber(): void

Asserts that a value is a number.

toBeObject(): void

Asserts that a value is an object.

toBeOdd(): void

Asserts that a number is odd.

toBeOneOf(expected: unknown[] | Iterable<unknown, any, any>): void

Asserts that the value is deep equal to an element in the expected array.

The value must be an array or iterable, which includes strings.

expected

the expected value

toBePositive(): void

Asserts that a value is a positive number.

toBeString(): void

Asserts that a value is a string.

toBeSymbol(): void

Asserts that a value is a symbol.

toBeTrue(): void

Asserts that a value is true.

toBeTruthy(): void

Asserts that a value is "truthy".

To assert that a value equals true, use toBe(true) instead.

toBeTypeOf(type: 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function'): void

Asserts that a value matches a specific type.

toBeUndefined(): void

Asserts that a value is undefined.

toBeValidDate(): void

Asserts that a value is a valid Date object.

toBeWithin(start: number, end: number): void

Asserts that a value is a number between a start and end value.

start

the start number (inclusive)

end

the end number (exclusive)

toContain(expected: unknown): void

Asserts that a value contains what is expected.

The value must be an array or iterable, which includes strings.

expected

the expected value

toContainAllKeys(expected: unknown): void

Asserts that an object contains all the provided keys.

The value must be an object

expected

the expected value

toContainAllValues(expected: unknown): void

Asserts that an object contain all the provided values.

The value must be an object

expected

the expected value

toContainAnyKeys(expected: unknown): void

Asserts that an object contains at least one of the provided keys. Asserts that an object contains all the provided keys.

The value must be an object

expected

the expected value

toContainAnyValues(expected: unknown): void

Asserts that an object contain any provided value.

The value must be an object

expected

the expected value

toContainEqual(expected: unknown): void

Asserts that a value contains and equals what is expected.

This matcher will perform a deep equality check for members of arrays, rather than checking for object identity.

expected

the expected value

toContainKey(expected: unknown): void

Asserts that an object contains a key.

The value must be an object

expected

the expected value

toContainKeys(expected: unknown): void

Asserts that an object contains all the provided keys.

expected

the expected value

toContainValue(expected: unknown): void

Asserts that an object contain the provided value.

The value must be an object

expected

the expected value

toContainValues(expected: unknown): void

Asserts that an object contain the provided value.

The value must be an object

expected

the expected value

toEndWith(expected: string): void

Asserts that a value ends with a string.

expected

the string to end with

toEqual(expected: T): void

Asserts that a value is deeply equal to what is expected.

expected

the expected value

toEqualIgnoringWhitespace(expected: string): void

Asserts that a value is equal to the expected string, ignoring any whitespace.

expected

the expected string

toHaveBeenCalled(): void

Ensures that a mock function is called.

toHaveBeenCalledTimes(expected: number): void

Ensures that a mock function is called an exact number of times.

toHaveBeenCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments.

toHaveBeenLastCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the last call.

toHaveBeenNthCalledWith(n: number, expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the nth call.

toHaveLength(length: number): void

Asserts that a value has a .length property that is equal to the expected length.

length

the expected length

toHaveProperty(keyPath: string | number | string | number[], value?: unknown): void

Asserts that a value has a property with the expected name, and value if provided.

keyPath

the expected property name or path, or an index

value

the expected property value, if provided

toHaveReturned(): void

Ensures that a mock function has returned successfully at least once.

A promise that is unfulfilled will be considered a failure. If the function threw an error, it will be considered a failure.

toHaveReturnedTimes(times: number): void

Ensures that a mock function has returned successfully at times times.

A promise that is unfulfilled will be considered a failure. If the function threw an error, it will be considered a failure.

toInclude(expected: string): void

Asserts that a value includes a string.

For non-string values, use toContain() instead.

expected

the expected substring

toIncludeRepeated(expected: string, times: number): void

Asserts that a value includes a string {times} times.

expected

the expected substring

times

the number of times the substring should occur

toMatch(expected: string | RegExp): void

Asserts that a value matches a regular expression or includes a substring.

expected

the expected substring or pattern.

toMatchInlineSnapshot(value?: string): void

Asserts that a value matches the most recent inline snapshot.

value

The latest automatically-updated snapshot value.

toMatchObject(subset: object): void

Asserts that an object matches a subset of properties.

subset

Subset of properties to match with.

toMatchSnapshot(hint?: string): void

Asserts that a value matches the most recent snapshot.

hint

Hint used to identify the snapshot in the snapshot file.

toSatisfy(predicate: (value: T) => boolean): void

Checks whether a value satisfies a custom condition.

predicate

The custom condition to be satisfied. It should be a function that takes a value as an argument (in this case the value from expect) and returns a boolean.

toStartWith(expected: string): void

Asserts that a value starts with a string.

expected

the string to start with

toStrictEqual(expected: T): void

Asserts that a value is deeply and strictly equal to what is expected.

There are two key differences from toEqual():

  1. It checks that the class is the same.
  2. It checks that undefined values match as well.
expected

the expected value

toThrow(expected?: unknown): void

Asserts that a function throws an error.

  • If expected is a string or RegExp, it will check the message property.
  • If expected is an Error object, it will check the name and message properties.
  • If expected is an Error constructor, it will check the class of the Error.
  • If expected is not provided, it will check if anything has thrown.
expected

the expected error, error message, or error pattern

toThrowError(expected?: unknown): void

Asserts that a function throws an error.

  • If expected is a string or RegExp, it will check the message property.
  • If expected is an Error object, it will check the name and message properties.
  • If expected is an Error constructor, it will check the class of the Error.
  • If expected is not provided, it will check if anything has thrown.
expected

the expected error, error message, or error pattern

toThrowErrorMatchingInlineSnapshot(value?: string): void

Asserts that a function throws an error matching the most recent snapshot.

value

The latest automatically-updated snapshot value.

toThrowErrorMatchingSnapshot(hint?: string): void

Asserts that a function throws an error matching the most recent snapshot.

interfaceMatchersBuiltin<T=unknown>

fail: (message?: string) => void

Assertion which fails.

not: Matchers<unknown>

Negates the result of a subsequent assertion. If you know how to test something, .not lets you test its opposite.

pass: (message?: string) => void

Assertion which passes.

rejects: Matchers<unknown>

Expects the value to be a promise that rejects.

resolves: Matchers<Awaited<T>>

Expects the value to be a promise that resolves.

lastCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the nth call.

nthCalledWith(n: number, expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the nth call.

toBe(expected: T): void

Asserts that a value equals what is expected.

  • For non-primitive values, like objects and arrays, use toEqual() instead.
  • For floating-point numbers, use toBeCloseTo() instead.
expected

the expected value

toBeArray(): void

Asserts that a value is a array.

toBeArrayOfSize(size: number): void

Asserts that a value is a array of a certain length.

toBeBoolean(): void

Asserts that a value is a boolean.

toBeCalled(): void

Ensures that a mock function is called an exact number of times.

toBeCalledTimes(expected: number): void

Ensure that a mock function is called with specific arguments.

toBeCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments.

toBeCloseTo(expected: number, numDigits?: number): void

Asserts that value is close to the expected by floating point precision.

For example, the following fails because arithmetic on decimal (base 10) values often have rounding errors in limited precision binary (base 2) representation.

expected

the expected value

numDigits

the number of digits to check after the decimal point. Default is 2

toBeDate(): void

Asserts that a value is a Date object.

To check if a date is valid, use toBeValidDate() instead.

toBeDefined(): void

Asserts that a value is defined. (e.g. is not undefined)

toBeEmpty(): void

Asserts that a value is empty.

toBeEmptyObject(): void

Asserts that a value is an empty object.

toBeEven(): void

Asserts that a number is even.

toBeFalse(): void

Asserts that a value is false.

toBeFalsy(): void

Asserts that a value is "falsy".

To assert that a value equals false, use toBe(false) instead.

toBeFinite(): void

Asserts that a value is a number, and is not NaN or Infinity.

toBeFunction(): void

Asserts that a value is a function.

toBeGreaterThan(expected: number | bigint): void

Asserts that a value is a number and is greater than the expected value.

expected

the expected number

toBeGreaterThanOrEqual(expected: number | bigint): void

Asserts that a value is a number and is greater than or equal to the expected value.

expected

the expected number

toBeInstanceOf(value: unknown): void

Asserts that the expected value is an instance of value

toBeInteger(): void

Asserts that a value is a number, and is an integer.

toBeLessThan(expected: number | bigint): void

Asserts that a value is a number and is less than the expected value.

expected

the expected number

toBeLessThanOrEqual(expected: number | bigint): void

Asserts that a value is a number and is less than or equal to the expected value.

expected

the expected number

toBeNaN(): void

Asserts that a value is NaN.

Same as using Number.isNaN().

toBeNegative(): void

Asserts that a value is a negative number.

toBeNil(): void

Asserts that a value is null or undefined.

toBeNull(): void

Asserts that a value is null.

toBeNumber(): void

Asserts that a value is a number.

toBeObject(): void

Asserts that a value is an object.

toBeOdd(): void

Asserts that a number is odd.

toBeOneOf(expected: unknown[] | Iterable<unknown, any, any>): void

Asserts that the value is deep equal to an element in the expected array.

The value must be an array or iterable, which includes strings.

expected

the expected value

toBePositive(): void

Asserts that a value is a positive number.

toBeString(): void

Asserts that a value is a string.

toBeSymbol(): void

Asserts that a value is a symbol.

toBeTrue(): void

Asserts that a value is true.

toBeTruthy(): void

Asserts that a value is "truthy".

To assert that a value equals true, use toBe(true) instead.

toBeTypeOf(type: 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function'): void

Asserts that a value matches a specific type.

toBeUndefined(): void

Asserts that a value is undefined.

toBeValidDate(): void

Asserts that a value is a valid Date object.

toBeWithin(start: number, end: number): void

Asserts that a value is a number between a start and end value.

start

the start number (inclusive)

end

the end number (exclusive)

toContain(expected: unknown): void

Asserts that a value contains what is expected.

The value must be an array or iterable, which includes strings.

expected

the expected value

toContainAllKeys(expected: unknown): void

Asserts that an object contains all the provided keys.

The value must be an object

expected

the expected value

toContainAllValues(expected: unknown): void

Asserts that an object contain all the provided values.

The value must be an object

expected

the expected value

toContainAnyKeys(expected: unknown): void

Asserts that an object contains at least one of the provided keys. Asserts that an object contains all the provided keys.

The value must be an object

expected

the expected value

toContainAnyValues(expected: unknown): void

Asserts that an object contain any provided value.

The value must be an object

expected

the expected value

toContainEqual(expected: unknown): void

Asserts that a value contains and equals what is expected.

This matcher will perform a deep equality check for members of arrays, rather than checking for object identity.

expected

the expected value

toContainKey(expected: unknown): void

Asserts that an object contains a key.

The value must be an object

expected

the expected value

toContainKeys(expected: unknown): void

Asserts that an object contains all the provided keys.

expected

the expected value

toContainValue(expected: unknown): void

Asserts that an object contain the provided value.

The value must be an object

expected

the expected value

toContainValues(expected: unknown): void

Asserts that an object contain the provided value.

The value must be an object

expected

the expected value

toEndWith(expected: string): void

Asserts that a value ends with a string.

expected

the string to end with

toEqual(expected: T): void

Asserts that a value is deeply equal to what is expected.

expected

the expected value

toEqualIgnoringWhitespace(expected: string): void

Asserts that a value is equal to the expected string, ignoring any whitespace.

expected

the expected string

toHaveBeenCalled(): void

Ensures that a mock function is called.

toHaveBeenCalledTimes(expected: number): void

Ensures that a mock function is called an exact number of times.

toHaveBeenCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments.

toHaveBeenLastCalledWith(expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the last call.

toHaveBeenNthCalledWith(n: number, expected: unknown[]): void

Ensure that a mock function is called with specific arguments for the nth call.

toHaveLength(length: number): void

Asserts that a value has a .length property that is equal to the expected length.

length

the expected length

toHaveProperty(keyPath: string | number | string | number[], value?: unknown): void

Asserts that a value has a property with the expected name, and value if provided.

keyPath

the expected property name or path, or an index

value

the expected property value, if provided

toHaveReturned(): void

Ensures that a mock function has returned successfully at least once.

A promise that is unfulfilled will be considered a failure. If the function threw an error, it will be considered a failure.

toHaveReturnedTimes(times: number): void

Ensures that a mock function has returned successfully at times times.

A promise that is unfulfilled will be considered a failure. If the function threw an error, it will be considered a failure.

toInclude(expected: string): void

Asserts that a value includes a string.

For non-string values, use toContain() instead.

expected

the expected substring

toIncludeRepeated(expected: string, times: number): void

Asserts that a value includes a string {times} times.

expected

the expected substring

times

the number of times the substring should occur

toMatch(expected: string | RegExp): void

Asserts that a value matches a regular expression or includes a substring.

expected

the expected substring or pattern.

toMatchInlineSnapshot(value?: string): void

Asserts that a value matches the most recent inline snapshot.

value

The latest automatically-updated snapshot value.

toMatchObject(subset: object): void

Asserts that an object matches a subset of properties.

subset

Subset of properties to match with.

toMatchSnapshot(hint?: string): void

Asserts that a value matches the most recent snapshot.

hint

Hint used to identify the snapshot in the snapshot file.

toSatisfy(predicate: (value: T) => boolean): void

Checks whether a value satisfies a custom condition.

predicate

The custom condition to be satisfied. It should be a function that takes a value as an argument (in this case the value from expect) and returns a boolean.

toStartWith(expected: string): void

Asserts that a value starts with a string.

expected

the string to start with

toStrictEqual(expected: T): void

Asserts that a value is deeply and strictly equal to what is expected.

There are two key differences from toEqual():

  1. It checks that the class is the same.
  2. It checks that undefined values match as well.
expected

the expected value

toThrow(expected?: unknown): void

Asserts that a function throws an error.

  • If expected is a string or RegExp, it will check the message property.
  • If expected is an Error object, it will check the name and message properties.
  • If expected is an Error constructor, it will check the class of the Error.
  • If expected is not provided, it will check if anything has thrown.
expected

the expected error, error message, or error pattern

toThrowError(expected?: unknown): void

Asserts that a function throws an error.

  • If expected is a string or RegExp, it will check the message property.
  • If expected is an Error object, it will check the name and message properties.
  • If expected is an Error constructor, it will check the class of the Error.
  • If expected is not provided, it will check if anything has thrown.
expected

the expected error, error message, or error pattern

toThrowErrorMatchingInlineSnapshot(value?: string): void

Asserts that a function throws an error matching the most recent snapshot.

value

The latest automatically-updated snapshot value.

toThrowErrorMatchingSnapshot(hint?: string): void

Asserts that a function throws an error matching the most recent snapshot.

typeMock<T extends (args: any[]) => any>=JestMock.Mock<T>

-- Mocks --

interfaceTest

Runs a test.

(label: string, fn: () => void | Promise<unknown> | (done: (err?: unknown) => void) => void, options?: number | TestOptions) => void;

typeTester
={
}

Custom equality tester

interfaceTestOptions

repeats: number

Sets the number of times to repeat the test, regardless of whether it passed or failed.

retry: number

Sets the number of times to retry the test if it fails.

timeout: number

Sets the timeout for the test in milliseconds.

If the test does not complete within this time, the test will fail with:

'Timeout: test {name} timed out after 5000ms'