From 4f6eaef2ace9bddd753750ec7bb46f74656a5211 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 27 Jun 2026 21:20:46 +0000 Subject: [PATCH 1/4] bun:test: deprecate legacy Jest matcher aliases in types and docs Jest 30 removed the alias matchers (toThrowError, toBeCalled, toBeCalledTimes, toBeCalledWith, lastCalledWith, nthCalledWith, toReturn, lastReturnedWith, nthReturnedWith). Bun still accepts them at runtime, so a suite using them passes under bun test but is a hard TypeError under Jest 30. Runtime support is kept (removal would be a breaking change and Vitest still accepts them), but each alias is now marked @deprecated in the bun:test type definitions with a pointer to the canonical matcher, and the matcher docs gain a table that lists the aliases explicitly as a deliberate superset of Jest 30. Also declares the three return-value aliases that were already wired up at runtime but missing from the types, fixes three copy/paste errors in existing @alias tags, and updates JSDoc examples that used toBeCalledWith to the canonical name. A describe block in expect.test.js exercises all nine aliases (skipped when the file is run under Jest). --- docs/test/writing-tests.mdx | 16 ++++++++++++ packages/bun-types/test.d.ts | 45 ++++++++++++++++++++++++++------- test/js/bun/test/expect.test.js | 34 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/docs/test/writing-tests.mdx b/docs/test/writing-tests.mdx index 270d8a4707ab..aec0c4d96797 100644 --- a/docs/test/writing-tests.mdx +++ b/docs/test/writing-tests.mdx @@ -581,6 +581,22 @@ Bun implements the following matchers. Full Jest compatibility is planned; see t | ------ | -------------------------- | | ❌ | `.addSnapshotSerializer()` | +### Legacy Jest Aliases + +Jest 30 removed the following matcher aliases. Bun continues to accept them at runtime so existing test suites keep working, but they 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()` | + ## Best Practices ### Use Descriptive Test Names diff --git a/packages/bun-types/test.d.ts b/packages/bun-types/test.d.ts index 38a6b3928b74..e4028ea55244 100644 --- a/packages/bun-types/test.d.ts +++ b/packages/bun-types/test.d.ts @@ -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 * @@ -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: * @@ -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. */ @@ -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; @@ -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. * @@ -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. @@ -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. */ @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; } diff --git a/test/js/bun/test/expect.test.js b/test/js/bun/test/expect.test.js index 114876e4d747..42adce12fb1e 100644 --- a/test/js/bun/test/expect.test.js +++ b/test/js/bun/test/expect.test.js @@ -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); + }); + }); }); function tmpFile(exists) { From 8af9a3ee8338818235ed05a238701ba2acdd466c Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 27 Jun 2026 22:38:22 +0000 Subject: [PATCH 2/4] test: exercise the legacy Jest matcher aliases in the bun-types fixture The runtime test in expect.test.js proves the aliases work at runtime, but it type-checks against the in-repo module, not the packed bun-types tarball that the bun-types integration test validates. Add a fixture block so that removing or renaming any of the alias declarations (in particular toReturn, lastReturnedWith, and nthReturnedWith, which were newly declared in this PR) fails the type integration test. Before the test.d.ts change this fixture fails with: TS2339: Property 'toReturn' does not exist on type 'Matchers<...>' TS2339: Property 'lastReturnedWith' does not exist on type 'Matchers<...>' TS2551: Property 'nthReturnedWith' does not exist on type 'Matchers<...>' --- test/integration/bun-types/fixture/test.ts | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/integration/bun-types/fixture/test.ts b/test/integration/bun-types/fixture/test.ts index 8725d1db569a..1be33eba1e11 100644 --- a/test/integration/bun-types/fixture/test.ts +++ b/test/integration/bun-types/fixture/test.ts @@ -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); From d6625e9f64ce74bc24c0b11ddb31d79abf956d9b Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 27 Jun 2026 22:40:24 +0000 Subject: [PATCH 3/4] docs: scope the legacy alias table to Bun's supported set The lead-in read as if the 9-row table were the complete list of aliases Jest 30 removed. Jest 30 removed 11; Bun does not implement toReturnTimes or toReturnWith, so reword the sentence to describe the aliases Bun supports rather than Jest's removal set. --- docs/test/writing-tests.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/test/writing-tests.mdx b/docs/test/writing-tests.mdx index aec0c4d96797..d4d9916808ce 100644 --- a/docs/test/writing-tests.mdx +++ b/docs/test/writing-tests.mdx @@ -583,7 +583,7 @@ Bun implements the following matchers. Full Jest compatibility is planned; see t ### Legacy Jest Aliases -Jest 30 removed the following matcher aliases. Bun continues to accept them at runtime so existing test suites keep working, but they are marked `@deprecated` in the type definitions. Prefer the canonical names so your tests run under Bun, Jest 30, and Vitest without changes. +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 | | --------------------- | ----------------------------- | From 84ce8e3236b9ce099ff873c1c2e9646a645679d5 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 27 Jun 2026 23:50:59 +0000 Subject: [PATCH 4/4] ci: retrigger