Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): fix toHaveBeenNthCalledWith error message when not called #5420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down
30 changes: 30 additions & 0 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down