Runs a function, once, after all the tests.
This is useful for running clean up tasks, like closing a socket or deleting temporary files.
Module
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
.
Runs a function, once, after all the tests.
This is useful for running clean up tasks, like closing a socket or deleting temporary files.
the function to run
Runs a function after each test.
This is useful for running clean up tasks, like closing a socket or deleting temporary files.
the function to run
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.
the function to run
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.
the function to run
Replace the module id
with the return value of factory
.
This is useful for mocking modules.
module ID to mock
a function returning an object that will be used as the exports of the mocked module
Restore the previous value of mocks.
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).
the number of milliseconds for the default timeout
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.
The time to set the system time to. If not provided, the system time will be reset.
this
Object representing an asymmetric matcher obtained by an static call to expect like expect.anything()
, expect.stringContaining("...")
, etc.
You can extend this interface with declaration merging, in order to add type support for custom asymmetric matchers.
Matches anything that was created with the given constructor. You can use it inside toEqual
or toBeCalledWith
instead of a literal value.
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:
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.
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
).
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.
Matches any received string that contains the exact expected string
Matches any string that contains the exact provided string
Matches anything that was created with the given constructor. You can use it inside toEqual
or toBeCalledWith
instead of a literal value.
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:
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.
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
).
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.
Matches any received string that contains the exact expected string
Matches any string that contains the exact provided string
All non-builtin matchers and asymmetric matchers that have been type-registered through declaration merging
You can extend this interface with declaration merging, in order to add type support for custom asymmetric matchers.
If the types has been defined through declaration merging, enforce it. Otherwise enforce the generic custom matcher signature.
Constructs the type of a mock function, e.g. the return type of jest.fn()
.
Constructs the type of a spied class or function.
Constructs the type of a spied class.
Constructs the type of a spied function.
Constructs the type of a spied getter.
Constructs the type of a spied setter.
You can extend this interface with declaration merging, in order to add type support for custom matchers.
Assertion which fails.
Negates the result of a subsequent assertion. If you know how to test something, .not
lets you test its opposite.
Assertion which passes.
Ensure that a mock function is called with specific arguments for the nth call.
Ensure that a mock function is called with specific arguments for the nth call.
Asserts that a value equals what is expected.
toEqual()
instead.toBeCloseTo()
instead.the expected value
Ensure that a mock function is called with specific arguments.
Ensure that a mock function is called with specific arguments.
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.
the expected value
the number of digits to check after the decimal point. Default is 2
Asserts that a value is a Date
object.
To check if a date is valid, use toBeValidDate()
instead.
Asserts that a value is "falsy".
To assert that a value equals false
, use toBe(false)
instead.
Asserts that a value is a number
and is greater than the expected value.
the expected number
Asserts that a value is a number
and is greater than or equal to the expected value.
the expected number
Asserts that a value is a number
and is less than the expected value.
the expected number
Asserts that a value is a number
and is less than or equal to the expected value.
the expected number
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.
the expected value
Asserts that a value is "truthy".
To assert that a value equals true
, use toBe(true)
instead.
Asserts that a value matches a specific type.
Asserts that a value is a number between a start and end value.
the start number (inclusive)
the end number (exclusive)
Asserts that a value contains what is expected.
The value must be an array or iterable, which includes strings.
the expected value
Asserts that an object
contains all the provided keys.
The value must be an object
the expected value
Asserts that an object
contain all the provided values.
The value must be an object
the expected value
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
the expected value
Asserts that an object
contain any provided value.
The value must be an object
the expected value
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.
the expected value
Asserts that an object
contains a key.
The value must be an object
the expected value
Asserts that an object
contains all the provided keys.
the expected value
Asserts that an object
contain the provided value.
The value must be an object
the expected value
Asserts that an object
contain the provided value.
The value must be an object
the expected value
Asserts that a value ends with a string
.
the string to end with
Asserts that a value is deeply equal to what is expected.
the expected value
Asserts that a value is equal to the expected string, ignoring any whitespace.
the expected string
Ensures that a mock function is called an exact number of times.
Ensure that a mock function is called with specific arguments.
Ensure that a mock function is called with specific arguments for the last call.
Ensure that a mock function is called with specific arguments for the nth call.
Asserts that a value has a .length
property that is equal to the expected length.
the expected length
Asserts that a value has a property with the expected name, and value if provided.
the expected property name or path, or an index
the expected property value, if provided
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.
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.
Asserts that a value includes a string
.
For non-string values, use toContain()
instead.
the expected substring
Asserts that a value includes a string
{times} times.
the expected substring
the number of times the substring should occur
Asserts that a value matches a regular expression or includes a substring.
the expected substring or pattern.
Asserts that a value matches the most recent inline snapshot.
The latest automatically-updated snapshot value.
Asserts that an object matches a subset of properties.
Subset of properties to match with.
Asserts that a value matches the most recent snapshot.
Hint used to identify the snapshot in the snapshot file.
Checks whether a value satisfies a custom condition.
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.
Asserts that a value starts with a string
.
the string to start with
Asserts that a value is deeply and strictly equal to what is expected.
There are two key differences from toEqual()
:
undefined
values match as well.the expected value
Asserts that a function throws an error.
string
or RegExp
, it will check the message
property.Error
object, it will check the name
and message
properties.Error
constructor, it will check the class of the Error
.the expected error, error message, or error pattern
Asserts that a function throws an error.
string
or RegExp
, it will check the message
property.Error
object, it will check the name
and message
properties.Error
constructor, it will check the class of the Error
.the expected error, error message, or error pattern
Asserts that a function throws an error matching the most recent snapshot.
The latest automatically-updated snapshot value.
Asserts that a function throws an error matching the most recent snapshot.
Assertion which fails.
Negates the result of a subsequent assertion. If you know how to test something, .not
lets you test its opposite.
Assertion which passes.
Ensure that a mock function is called with specific arguments for the nth call.
Ensure that a mock function is called with specific arguments for the nth call.
Asserts that a value equals what is expected.
toEqual()
instead.toBeCloseTo()
instead.the expected value
Ensure that a mock function is called with specific arguments.
Ensure that a mock function is called with specific arguments.
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.
the expected value
the number of digits to check after the decimal point. Default is 2
Asserts that a value is a Date
object.
To check if a date is valid, use toBeValidDate()
instead.
Asserts that a value is "falsy".
To assert that a value equals false
, use toBe(false)
instead.
Asserts that a value is a number
and is greater than the expected value.
the expected number
Asserts that a value is a number
and is greater than or equal to the expected value.
the expected number
Asserts that a value is a number
and is less than the expected value.
the expected number
Asserts that a value is a number
and is less than or equal to the expected value.
the expected number
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.
the expected value
Asserts that a value is "truthy".
To assert that a value equals true
, use toBe(true)
instead.
Asserts that a value matches a specific type.
Asserts that a value is a number between a start and end value.
the start number (inclusive)
the end number (exclusive)
Asserts that a value contains what is expected.
The value must be an array or iterable, which includes strings.
the expected value
Asserts that an object
contains all the provided keys.
The value must be an object
the expected value
Asserts that an object
contain all the provided values.
The value must be an object
the expected value
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
the expected value
Asserts that an object
contain any provided value.
The value must be an object
the expected value
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.
the expected value
Asserts that an object
contains a key.
The value must be an object
the expected value
Asserts that an object
contains all the provided keys.
the expected value
Asserts that an object
contain the provided value.
The value must be an object
the expected value
Asserts that an object
contain the provided value.
The value must be an object
the expected value
Asserts that a value ends with a string
.
the string to end with
Asserts that a value is deeply equal to what is expected.
the expected value
Asserts that a value is equal to the expected string, ignoring any whitespace.
the expected string
Ensures that a mock function is called an exact number of times.
Ensure that a mock function is called with specific arguments.
Ensure that a mock function is called with specific arguments for the last call.
Ensure that a mock function is called with specific arguments for the nth call.
Asserts that a value has a .length
property that is equal to the expected length.
the expected length
Asserts that a value has a property with the expected name, and value if provided.
the expected property name or path, or an index
the expected property value, if provided
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.
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.
Asserts that a value includes a string
.
For non-string values, use toContain()
instead.
the expected substring
Asserts that a value includes a string
{times} times.
the expected substring
the number of times the substring should occur
Asserts that a value matches a regular expression or includes a substring.
the expected substring or pattern.
Asserts that a value matches the most recent inline snapshot.
The latest automatically-updated snapshot value.
Asserts that an object matches a subset of properties.
Subset of properties to match with.
Asserts that a value matches the most recent snapshot.
Hint used to identify the snapshot in the snapshot file.
Checks whether a value satisfies a custom condition.
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.
Asserts that a value starts with a string
.
the string to start with
Asserts that a value is deeply and strictly equal to what is expected.
There are two key differences from toEqual()
:
undefined
values match as well.the expected value
Asserts that a function throws an error.
string
or RegExp
, it will check the message
property.Error
object, it will check the name
and message
properties.Error
constructor, it will check the class of the Error
.the expected error, error message, or error pattern
Asserts that a function throws an error.
string
or RegExp
, it will check the message
property.Error
object, it will check the name
and message
properties.Error
constructor, it will check the class of the Error
.the expected error, error message, or error pattern
Asserts that a function throws an error matching the most recent snapshot.
The latest automatically-updated snapshot value.
Asserts that a function throws an error matching the most recent snapshot.
Runs a test.
Custom equality tester
Sets the number of times to repeat the test, regardless of whether it passed or failed.
Sets the number of times to retry the test if it fails.
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'