Skip to content

Commit

Permalink
do not match when too many args provided
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson committed Oct 22, 2020
1 parent eb32c67 commit 26abfc6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ class WhenMock {
// Do not let a once mock match more than once
if (once && called) continue

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

if (isMatch) {
this.callMocks[i].called = true
return typeof returnValue === 'function' ? returnValue(...args) : returnValue
Expand Down
2 changes: 2 additions & 0 deletions src/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ describe('When', () => {
.mockReturnValue('x')

expect(fn(1, 'foo', true, 'whatever')).toEqual('x')
expect(fn(1, 'foo', true)).toEqual(undefined)
expect(fn(1, 'foo', true, 'whatever', undefined, 'oops')).toEqual(undefined)
expect(spyEquals).toBeCalledWith(1, 1)
expect(spyEquals).toBeCalledWith('foo', 'foo')
expect(spyEquals).toBeCalledWith(true, true)
Expand Down

0 comments on commit 26abfc6

Please sign in to comment.