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: fix toHaveBeenCalledWith(asymmetricMatcher) with undefined arguments #7624

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,19 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
throw new AssertionError(msg)
}
})

// manually compare array elements since `jestEquals` cannot
// apply assymetric matcher to `undefined` array element.
function equalsArgumentArray(a: unknown[], b: unknown[]) {
return a.length === b.length && a.every((aItem, i) =>
jestEquals(aItem, b[i], [...customTesters, iterableEquality]),
)
}

def(['toHaveBeenCalledWith', 'toBeCalledWith'], function (...args) {
const spy = getSpy(this)
const spyName = spy.getMockName()
const pass = spy.mock.calls.some(callArg =>
jestEquals(callArg, args, [...customTesters, iterableEquality]),
)
const pass = spy.mock.calls.some(callArg => equalsArgumentArray(callArg, args))
const isNot = utils.flag(this, 'negate') as boolean

const msg = utils.getMessage(this, [
Expand All @@ -599,9 +606,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const spy = getSpy(this)
const spyName = spy.getMockName()
const callCount = spy.mock.calls.length
const hasCallWithArgs = spy.mock.calls.some(callArg =>
jestEquals(callArg, args, [...customTesters, iterableEquality]),
)
const hasCallWithArgs = spy.mock.calls.some(callArg => equalsArgumentArray(callArg, args))
const pass = hasCallWithArgs && callCount === 1
const isNot = utils.flag(this, 'negate') as boolean

Expand All @@ -625,7 +630,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const callCount = spy.mock.calls.length
const isCalled = times <= callCount
this.assert(
jestEquals(nthCall, args, [...customTesters, iterableEquality]),
nthCall && equalsArgumentArray(nthCall, args),
`expected ${ordinalOf(
times,
)} "${spyName}" call to have been called with #{exp}${
Expand All @@ -648,7 +653,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const lastCall = spy.mock.calls[spy.mock.calls.length - 1]

this.assert(
jestEquals(lastCall, args, [...customTesters, iterableEquality]),
lastCall && equalsArgumentArray(lastCall, args),
`expected last "${spyName}" call to have been called with #{exp}`,
`expected last "${spyName}" call to not have been called with #{exp}`,
args,
Expand Down
26 changes: 26 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,32 @@ describe('toHaveBeenCalled', () => {
}).toThrow(/^expected "spy" to not be called at all[^e]/)
})
})

it('undefined argument', () => {
const fn = vi.fn()
fn(undefined)
expect(fn).not.toHaveBeenCalledWith()
expect(fn).toHaveBeenCalledWith(undefined)
expect(fn).toHaveBeenCalledWith(expect.toSatisfy(() => true))
expect(fn).toHaveBeenCalledWith(expect.not.toSatisfy(() => false))
expect(fn).toHaveBeenCalledWith(expect.toBeOneOf([undefined, null]))
})

it('no argument', () => {
const fn = vi.fn()
fn()
expect(fn).toHaveBeenCalledWith()
expect(fn).not.toHaveBeenCalledWith(undefined)
expect(fn).not.toHaveBeenCalledWith(expect.toSatisfy(() => true))
expect(fn).not.toHaveBeenCalledWith(expect.not.toSatisfy(() => false))
expect(fn).not.toHaveBeenCalledWith(expect.toBeOneOf([undefined, null]))
})

it('no strict equal check for each argument', () => {
const fn = vi.fn()
fn({ x: undefined, z: 123 })
expect(fn).toHaveBeenCalledWith({ y: undefined, z: 123 })
})
})

describe('toHaveBeenCalledWith', () => {
Expand Down
Loading