Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes timkindberg/jest-when#89 #93

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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