diff --git a/docs/test/writing-tests.mdx b/docs/test/writing-tests.mdx index 270d8a4707ab..d4d9916808ce 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 + +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()` | + ## 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/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); 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) {