Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/test/writing-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,22 @@ Bun implements the following matchers. Full Jest compatibility is planned; see t
| ------ | -------------------------- |
| ❌ | `.addSnapshotSerializer()` |

### Legacy Jest Aliases

Bun supports the following legacy matcher aliases that were removed in Jest 30. They remain available at runtime so existing test suites keep working, but are marked `@deprecated` in the type definitions. Prefer the canonical names so your tests run under Bun, Jest 30, and Vitest without changes.

| Alias (deprecated) | Use instead |
| --------------------- | ----------------------------- |
| `.toThrowError()` | `.toThrow()` |
| `.toBeCalled()` | `.toHaveBeenCalled()` |
| `.toBeCalledTimes()` | `.toHaveBeenCalledTimes()` |
| `.toBeCalledWith()` | `.toHaveBeenCalledWith()` |
| `.lastCalledWith()` | `.toHaveBeenLastCalledWith()` |
| `.nthCalledWith()` | `.toHaveBeenNthCalledWith()` |
| `.toReturn()` | `.toHaveReturned()` |
| `.lastReturnedWith()` | `.toHaveLastReturnedWith()` |
| `.nthReturnedWith()` | `.toHaveNthReturnedWith()` |
Comment thread
robobun marked this conversation as resolved.
Resolved

## Best Practices

### Use Descriptive Test Names
Expand Down
45 changes: 36 additions & 9 deletions packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ declare module "bun:test" {
export interface AsymmetricMatchersBuiltin {
/**
* Matches anything that was created with the given constructor.
* Use it inside `toEqual` or `toBeCalledWith` instead of a literal value.
* Use it inside `toEqual` or `toHaveBeenCalledWith` instead of a literal value.
*
* @example
*
Expand All @@ -789,12 +789,12 @@ declare module "bun:test" {
* test('randocall calls its callback with a number', () => {
* const mock = jest.fn();
* randocall(mock);
* expect(mock).toBeCalledWith(expect.any(Number));
* expect(mock).toHaveBeenCalledWith(expect.any(Number));
* });
*/
any(constructor: ((...args: any[]) => any) | { new (...args: any[]): any }): AsymmetricMatcher;
/**
* Matches anything but null or undefined. Use it inside `toEqual` or `toBeCalledWith` instead
* Matches anything but null or undefined. Use it inside `toEqual` or `toHaveBeenCalledWith` instead
* of a literal value. For example, to check that a mock function is called with a
* non-null argument:
*
Expand All @@ -803,13 +803,13 @@ declare module "bun:test" {
* test('map calls its argument with a non-null argument', () => {
* const mock = jest.fn();
* [1].map(x => mock(x));
* expect(mock).toBeCalledWith(expect.anything());
* expect(mock).toHaveBeenCalledWith(expect.anything());
* });
*/
anything(): AsymmetricMatcher;
/**
* Matches any array made up entirely of elements in the provided array.
* Use it inside `toEqual` or `toBeCalledWith` instead of a literal value.
* Use it inside `toEqual` or `toHaveBeenCalledWith` instead of a literal value.
*
* Optionally, pass a type for the elements as a generic argument.
*/
Expand Down Expand Up @@ -1432,13 +1432,14 @@ declare module "bun:test" {
* function fail() {
* throw new Error("Oops!");
* }
* expect(fail).toThrowError("Oops!");
* expect(fail).toThrowError(/oops/i);
* expect(fail).toThrowError(Error);
* expect(fail).toThrowError();
* expect(fail).toThrow("Oops!");
* expect(fail).toThrow(/oops/i);
* expect(fail).toThrow(Error);
* expect(fail).toThrow();
*
* @param expected the expected error, error message, or error pattern
* @alias toThrow
* @deprecated Use {@link toThrow} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
toThrowError(expected?: unknown): void;

Expand Down Expand Up @@ -1814,6 +1815,13 @@ declare module "bun:test" {
*/
toHaveReturned(): void;

/**
* Ensures that a mock function has returned successfully at least once.
* @alias toHaveReturned
* @deprecated Use {@link toHaveReturned} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
toReturn(): void;

/**
* Ensures that a mock function has returned successfully `times` times.
*
Expand All @@ -1833,6 +1841,13 @@ declare module "bun:test" {
*/
toHaveLastReturnedWith(expected: unknown): void;

/**
* Ensures that a mock function has returned a specific value on its last invocation.
* @alias toHaveLastReturnedWith
* @deprecated Use {@link toHaveLastReturnedWith} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
lastReturnedWith(expected: unknown): void;

/**
* Ensures that a mock function has returned a specific value on the nth invocation.
* This matcher uses deep equality, like toEqual(), and supports asymmetric matchers.
Expand All @@ -1841,6 +1856,13 @@ declare module "bun:test" {
*/
toHaveNthReturnedWith(n: number, expected: unknown): void;

/**
* Ensures that a mock function has returned a specific value on the nth invocation.
* @alias toHaveNthReturnedWith
* @deprecated Use {@link toHaveNthReturnedWith} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
nthReturnedWith(n: number, expected: unknown): void;

/**
* Ensures that a mock function is called.
*/
Expand All @@ -1849,6 +1871,7 @@ declare module "bun:test" {
/**
* Ensures that a mock function is called.
* @alias toHaveBeenCalled
* @deprecated Use {@link toHaveBeenCalled} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
toBeCalled(): void;

Expand All @@ -1860,6 +1883,7 @@ declare module "bun:test" {
/**
* Ensures that a mock function is called an exact number of times.
* @alias toHaveBeenCalledTimes
* @deprecated Use {@link toHaveBeenCalledTimes} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
toBeCalledTimes(expected: number): void;

Expand All @@ -1871,6 +1895,7 @@ declare module "bun:test" {
/**
* Ensure that a mock function is called with specific arguments.
* @alias toHaveBeenCalledWith
* @deprecated Use {@link toHaveBeenCalledWith} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
toBeCalledWith(...expected: unknown[]): void;

Expand All @@ -1882,6 +1907,7 @@ declare module "bun:test" {
/**
* Ensure that a mock function is called with specific arguments for the last call.
* @alias toHaveBeenLastCalledWith
* @deprecated Use {@link toHaveBeenLastCalledWith} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
lastCalledWith(...expected: unknown[]): void;

Expand All @@ -1893,6 +1919,7 @@ declare module "bun:test" {
/**
* Ensure that a mock function is called with specific arguments for the nth call.
* @alias toHaveBeenNthCalledWith
* @deprecated Use {@link toHaveBeenNthCalledWith} instead. Jest removed this alias in Jest 30; Bun keeps it for backward compatibility.
*/
nthCalledWith(n: number, ...expected: unknown[]): void;
}
Expand Down
24 changes: 24 additions & 0 deletions test/integration/bun-types/fixture/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ describe("bun:test", () => {
expect(fail).toThrow(Error);
expect(fail).toThrow(new Error("Bad"));
});

// Jest 30 removed these aliases. Bun keeps them and declares them @deprecated.
test("legacy Jest matcher aliases (removed in Jest 30)", () => {
function fail() {
throw new Error("Bad");
}
expect(fail).toThrowError();
expect(fail).toThrowError("Bad");
expect(fail).toThrowError(/bad/i);
expect(fail).toThrowError(Error);
expect(fail).toThrowError(new Error("Bad"));

const fn = jest.fn((a: number) => a + 1);
fn(1);
fn(2);
expect(fn).toBeCalled();
expect(fn).toBeCalledTimes(2);
expect(fn).toBeCalledWith(1);
expect(fn).lastCalledWith(2);
expect(fn).nthCalledWith(1, 1);
expect(fn).toReturn();
expect(fn).lastReturnedWith(3);
expect(fn).nthReturnedWith(1, 2);
});
});
test("expect()", () => {
expect(1).toBe(1);
Expand Down
34 changes: 34 additions & 0 deletions test/js/bun/test/expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4954,6 +4954,40 @@ describe("expect()", () => {
expect(e.message).toContain('"ball"');
}
});

// Jest 30 removed these alias matchers. Bun keeps them for backward
// compatibility and marks them @deprecated in its type definitions.
describe("legacy Jest matcher aliases (removed in Jest 30)", () => {
test_skipIf(isJest)("toThrowError aliases toThrow", () => {
expect(() => {
throw new Error("x");
}).toThrowError("x");
expect(() => {}).not.toThrowError();
});

test_skipIf(isJest)("mock call aliases", () => {
const fn = jest.fn((/** @type {number} */ a) => a + 1);
expect(fn).not.toBeCalled();
fn(1);
fn(2);
expect(fn).toBeCalled();
expect(fn).toBeCalledTimes(2);
expect(fn).toBeCalledWith(1);
expect(fn).lastCalledWith(2);
expect(fn).nthCalledWith(1, 1);
expect(fn).nthCalledWith(2, 2);
});

test_skipIf(isJest)("mock return aliases", () => {
const fn = jest.fn((/** @type {number} */ a) => a + 1);
fn(1);
fn(2);
expect(fn).toReturn();
expect(fn).lastReturnedWith(3);
expect(fn).nthReturnedWith(1, 2);
expect(fn).nthReturnedWith(2, 3);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});
});

function tmpFile(exists) {
Expand Down
Loading