[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: fix `toHaveBeenCalledWith(asymmetricMatcher)` with `undefined` arguments (#7624)

authored by

Hiroshi Ogawa and committed by
GitHub
(Mar 7, 2025, 9:44 AM +0100) 0fb21faa 6ece0d9e

+39 -8
+13 -8
packages/expect/src/jest-expect.ts
··· 576 576 throw new AssertionError(msg) 577 577 } 578 578 }) 579 + 580 + // manually compare array elements since `jestEquals` cannot 581 + // apply assymetric matcher to `undefined` array element. 582 + function equalsArgumentArray(a: unknown[], b: unknown[]) { 583 + return a.length === b.length && a.every((aItem, i) => 584 + jestEquals(aItem, b[i], [...customTesters, iterableEquality]), 585 + ) 586 + } 587 + 579 588 def(['toHaveBeenCalledWith', 'toBeCalledWith'], function (...args) { 580 589 const spy = getSpy(this) 581 590 const spyName = spy.getMockName() 582 - const pass = spy.mock.calls.some(callArg => 583 - jestEquals(callArg, args, [...customTesters, iterableEquality]), 584 - ) 591 + const pass = spy.mock.calls.some(callArg => equalsArgumentArray(callArg, args)) 585 592 const isNot = utils.flag(this, 'negate') as boolean 586 593 587 594 const msg = utils.getMessage(this, [ ··· 599 606 const spy = getSpy(this) 600 607 const spyName = spy.getMockName() 601 608 const callCount = spy.mock.calls.length 602 - const hasCallWithArgs = spy.mock.calls.some(callArg => 603 - jestEquals(callArg, args, [...customTesters, iterableEquality]), 604 - ) 609 + const hasCallWithArgs = spy.mock.calls.some(callArg => equalsArgumentArray(callArg, args)) 605 610 const pass = hasCallWithArgs && callCount === 1 606 611 const isNot = utils.flag(this, 'negate') as boolean 607 612 ··· 625 630 const callCount = spy.mock.calls.length 626 631 const isCalled = times <= callCount 627 632 this.assert( 628 - jestEquals(nthCall, args, [...customTesters, iterableEquality]), 633 + nthCall && equalsArgumentArray(nthCall, args), 629 634 `expected ${ordinalOf( 630 635 times, 631 636 )} "${spyName}" call to have been called with #{exp}${ ··· 648 653 const lastCall = spy.mock.calls[spy.mock.calls.length - 1] 649 654 650 655 this.assert( 651 - jestEquals(lastCall, args, [...customTesters, iterableEquality]), 656 + lastCall && equalsArgumentArray(lastCall, args), 652 657 `expected last "${spyName}" call to have been called with #{exp}`, 653 658 `expected last "${spyName}" call to not have been called with #{exp}`, 654 659 args,
+26
test/core/test/jest-expect.test.ts
··· 715 715 }).toThrow(/^expected "spy" to not be called at all[^e]/) 716 716 }) 717 717 }) 718 + 719 + it('undefined argument', () => { 720 + const fn = vi.fn() 721 + fn(undefined) 722 + expect(fn).not.toHaveBeenCalledWith() 723 + expect(fn).toHaveBeenCalledWith(undefined) 724 + expect(fn).toHaveBeenCalledWith(expect.toSatisfy(() => true)) 725 + expect(fn).toHaveBeenCalledWith(expect.not.toSatisfy(() => false)) 726 + expect(fn).toHaveBeenCalledWith(expect.toBeOneOf([undefined, null])) 727 + }) 728 + 729 + it('no argument', () => { 730 + const fn = vi.fn() 731 + fn() 732 + expect(fn).toHaveBeenCalledWith() 733 + expect(fn).not.toHaveBeenCalledWith(undefined) 734 + expect(fn).not.toHaveBeenCalledWith(expect.toSatisfy(() => true)) 735 + expect(fn).not.toHaveBeenCalledWith(expect.not.toSatisfy(() => false)) 736 + expect(fn).not.toHaveBeenCalledWith(expect.toBeOneOf([undefined, null])) 737 + }) 738 + 739 + it('no strict equal check for each argument', () => { 740 + const fn = vi.fn() 741 + fn({ x: undefined, z: 123 }) 742 + expect(fn).toHaveBeenCalledWith({ y: undefined, z: 123 }) 743 + }) 718 744 }) 719 745 720 746 describe('toHaveBeenCalledWith', () => {