timkindberg / jest-when

Jest support for mock argument-matched return values.
MIT License
734 stars 38 forks source link

Fix: interoperability with @golevelup/ts-jest #89

Closed mjgp2 closed 2 years ago

mjgp2 commented 2 years ago

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 {
Djaler commented 2 years ago

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

timkindberg commented 2 years ago

Can someone make a PR with test for this pls?

mjgp2 commented 2 years ago

PR made: https://github.com/timkindberg/jest-when/pull/93

timkindberg commented 2 years ago

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.

mjgp2 commented 2 years ago

Brilliant, thanks!