From 26abfc62f43750a8c75d611ea11878b8312b167a Mon Sep 17 00:00:00 2001 From: Tom Jenkinson Date: Thu, 22 Oct 2020 17:59:56 +0100 Subject: [PATCH] do not match when too many args provided --- src/when.js | 5 ++++- src/when.test.js | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/when.js b/src/when.js index c3614c0..543a7ff 100644 --- a/src/when.js +++ b/src/when.js @@ -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 diff --git a/src/when.test.js b/src/when.test.js index de9bbf0..b408315 100644 --- a/src/when.test.js +++ b/src/when.test.js @@ -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)