timkindberg / jest-when

Jest support for mock argument-matched return values.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix: interoperability with @golevelup/ts-jest

mjgp2 opened this issue Β· comments

Hi! πŸ‘‹

I had a problem using createMock from @golevelup/ts-jest because it uses proxies to fake properties, and the test below against _isAllArgsFunctionMatcher gives a false positive (returns a jest.fn() instance).

Here is the diff that solved my problem:

diff --git a/node_modules/jest-when/src/when.js b/node_modules/jest-when/src/when.js
index 760b9bb..b7590bc 100644
--- a/node_modules/jest-when/src/when.js
+++ b/node_modules/jest-when/src/when.js
@@ -93,7 +93,7 @@ class WhenMock {
 
           let isMatch = false
 
-          if (matchers && matchers[0] && matchers[0]._isAllArgsFunctionMatcher) {
+          if (matchers && matchers[0] && (typeof matchers[0] === 'function' || typeof matchers[0] === 'object') && '_isAllArgsFunctionMatcher' in matchers[0] && matchers[0]._isAllArgsFunctionMatcher) {
             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 {

Same thing with jest-mock-extended. I think you should check more strictly, like matchers[0]._isAllArgsFunctionMatcher === true

Can someone make a PR with test for this pls?

PR made: #93

Sorry for the delay... I didn't fully understand this one because I don't use either of these two libraries so I needed some extra time to review. It's in v3.5.1.

Brilliant, thanks!