Skip to content

Commit

Permalink
fix matching exact args
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson committed Oct 29, 2020
1 parent b03f83b commit fa68b7f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WhenMock {
if (once && called) continue

const isMatch =
args.length <= matchers.length &&
args.length === matchers.length &&
matchers.reduce(checkArgumentMatchers(expectCall, args), true)

if (isMatch) {
Expand Down
5 changes: 2 additions & 3 deletions src/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,14 @@ describe('When', () => {
const anyString = expect.any(String)

when(fn)
.calledWith(1, 'foo', true, anyString, undefined)
.calledWith(1, 'foo', true, anyString)
.mockReturnValue('x')

expect(fn(1, 'foo', true, 'whatever')).toEqual('x')
expect(spyEquals).toBeCalledWith(1, 1)
expect(spyEquals).toBeCalledWith('foo', 'foo')
expect(spyEquals).toBeCalledWith(true, true)
expect(spyEquals).toBeCalledWith('whatever', anyString)
expect(spyEquals).toBeCalledWith(undefined, undefined)
})

it('only matches exact sets of args, too little or too many args do not trigger mock return', () => {
Expand All @@ -178,7 +177,7 @@ describe('When', () => {
.calledWith(1, 'foo', true, expect.any(String), undefined)
.mockReturnValue('x')

expect(fn(1, 'foo', true)).toEqual(undefined)
expect(fn(1, 'foo', true, 'whatever')).toEqual(undefined)
expect(fn(1, 'foo', true, 'whatever', undefined, 'oops')).toEqual(undefined)
})

Expand Down

0 comments on commit fa68b7f

Please # to comment.