Skip to content

Commit 562a35a

Browse files
author
mjgp2
committed
fixes #89
1 parent d4247dc commit 562a35a

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/when.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ class WhenMock {
9393

9494
let isMatch = false
9595

96-
if (matchers && matchers[0] && matchers[0]._isAllArgsFunctionMatcher) {
96+
if (matchers && matchers[0] &&
97+
// is a possible all args matcher object
98+
(typeof matchers[0] === 'function' || typeof matchers[0] === 'object') &&
99+
// ensure not a proxy
100+
'_isAllArgsFunctionMatcher' in matchers[0] &&
101+
// check for the special property name
102+
matchers[0]._isAllArgsFunctionMatcher === true
103+
) {
97104
if (matchers.length > 1) throw new Error('When using when.allArgs, it must be the one and only matcher provided to calledWith. You have incorrectly provided other matchers along with when.allArgs.')
98105
isMatch = checkArgumentMatchers(expectCall, [args])(true, matchers[0], 0)
99106
} else {

src/when.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,22 @@ describe('When', () => {
249249
expect(fn(123, 'not a number')).toBeUndefined()
250250
})
251251

252+
it('first matcher is a proxy', () => {
253+
const fn = jest.fn()
254+
255+
const proxy = new Proxy({}, {
256+
get: () => true
257+
})
258+
259+
when(fn)
260+
.calledWith(proxy)
261+
.mockReturnValue('x')
262+
263+
expect(proxy._isAllArgsFunctionMatcher).toEqual(true)
264+
265+
expect(fn(proxy)).toEqual('x')
266+
})
267+
252268
it('single arg match example', () => {
253269
const fn = jest.fn()
254270
const argAtIndex = (index, matcher) => when.allArgs((args, equals) => equals(args[index], matcher))

0 commit comments

Comments
 (0)