diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 1df2260926e8..7816f839299f 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -521,13 +521,16 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { const spy = getSpy(this) const spyName = spy.getMockName() const nthCall = spy.mock.calls[times - 1] - + const callCount = spy.mock.calls.length + const isCalled = times <= callCount this.assert( jestEquals(nthCall, args, [...customTesters, iterableEquality]), - `expected ${ordinalOf(times)} "${spyName}" call to have been called with #{exp}`, + `expected ${ordinalOf(times)} "${spyName}" call to have been called with #{exp}${ + isCalled ? `` : `, but called only ${callCount} times`}`, `expected ${ordinalOf(times)} "${spyName}" call to not have been called with #{exp}`, args, nthCall, + isCalled, ) }) def(['toHaveBeenLastCalledWith', 'lastCalledWith'], function (...args: any[]) { diff --git a/test/core/test/__snapshots__/jest-expect.test.ts.snap b/test/core/test/__snapshots__/jest-expect.test.ts.snap index d1f7b3d9b6de..3e92bdac56d4 100644 --- a/test/core/test/__snapshots__/jest-expect.test.ts.snap +++ b/test/core/test/__snapshots__/jest-expect.test.ts.snap @@ -311,6 +311,36 @@ exports[`asymmetric matcher error 23`] = ` } `; +exports[`toHaveBeenNthCalledWith error 1`] = ` +{ + "actual": "Array [ + "Hi", +]", + "diff": "- Expected ++ Received + + Array [ +- "hey", ++ "Hi", + ]", + "expected": "Array [ + "hey", +]", + "message": "expected 2nd "spy" call to have been called with [ 'hey' ]", +} +`; + +exports[`toHaveBeenNthCalledWith error 2`] = ` +{ + "actual": "undefined", + "diff": undefined, + "expected": "Array [ + "hey", +]", + "message": "expected 3rd "spy" call to have been called with [ 'hey' ], but called only 2 times", +} +`; + exports[`toMatch/toContain diff 1`] = ` { "actual": "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 990361d41146..4648f0ab3be9 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1085,6 +1085,14 @@ it('asymmetric matcher error', () => { }).toThrow(MyError1)) }) +it('toHaveBeenNthCalledWith error', () => { + const fn = vi.fn() + fn('World') + fn('Hi') + snapshotError(() => expect(fn).toHaveBeenNthCalledWith(2, 'hey')) + snapshotError(() => expect(fn).toHaveBeenNthCalledWith(3, 'hey')) +}) + it('toMatch/toContain diff', () => { snapshotError(() => expect('hello'.repeat(20)).toContain('world')) snapshotError(() => expect('hello'.repeat(20)).toMatch('world'))