Skip to content

Commit

Permalink
Merge pull request #93 from mjgp2/issue-89
Browse files Browse the repository at this point in the history
fixes #89
  • Loading branch information
timkindberg authored Feb 7, 2022
2 parents d4247dc + 562a35a commit 97b3725
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ class WhenMock {

let isMatch = false

if (matchers && matchers[0] && matchers[0]._isAllArgsFunctionMatcher) {
if (matchers && matchers[0] &&
// is a possible all args matcher object
(typeof matchers[0] === 'function' || typeof matchers[0] === 'object') &&
// ensure not a proxy
'_isAllArgsFunctionMatcher' in matchers[0] &&
// check for the special property name
matchers[0]._isAllArgsFunctionMatcher === true
) {
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.')
isMatch = checkArgumentMatchers(expectCall, [args])(true, matchers[0], 0)
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,22 @@ describe('When', () => {
expect(fn(123, 'not a number')).toBeUndefined()
})

it('first matcher is a proxy', () => {
const fn = jest.fn()

const proxy = new Proxy({}, {
get: () => true
})

when(fn)
.calledWith(proxy)
.mockReturnValue('x')

expect(proxy._isAllArgsFunctionMatcher).toEqual(true)

expect(fn(proxy)).toEqual('x')
})

it('single arg match example', () => {
const fn = jest.fn()
const argAtIndex = (index, matcher) => when.allArgs((args, equals) => equals(args[index], matcher))
Expand Down

0 comments on commit 97b3725

Please sign in to comment.