{ | ||
"name": "jest-when", | ||
"version": "3.4.0", | ||
"version": "3.4.1", | ||
"description": "An extension lib for jest", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
const assert = require('assert') | ||
const utils = require('expect/build/jasmineUtils') | ||
@@ -8,2 +7,19 @@ let registry = new Set() | ||
/** | ||
* A hack to capture a reference to the `equals` jasmineUtil | ||
*/ | ||
let equals = () => {} | ||
expect.extend({ | ||
__capture_equals__ () { | ||
equals = this.equals | ||
return { pass: true } | ||
} | ||
}) | ||
expect().__capture_equals__() | ||
let JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object') | ||
delete global[JEST_MATCHERS_OBJECT].matchers.__capture_equals__ | ||
/** | ||
* End hack | ||
*/ | ||
const checkArgumentMatchers = (expectCall, args) => (match, matcher, i) => { | ||
@@ -31,6 +47,6 @@ // Propagate failure to the end | ||
if (isFunctionMatcher) { | ||
return matcher(arg, utils.equals) | ||
return matcher(arg, equals) | ||
} | ||
return utils.equals(arg, matcher) | ||
return equals(arg, matcher) | ||
} | ||
@@ -58,3 +74,3 @@ | ||
this.callMocks = this.callMocks | ||
.filter((callMock) => once || callMock.once || !utils.equals(callMock.matchers, matchers)) | ||
.filter((callMock) => once || callMock.once || !equals(callMock.matchers, matchers)) | ||
.concat({ matchers, mockImplementation, expectCall, once, called: false, id: this.nextCallMockId, callLine: getCallLine() }) | ||
@@ -61,0 +77,0 @@ .sort((a, b) => { |
@@ -5,7 +5,5 @@ const errMsg = ({ expect, actual }) => | ||
describe('When', () => { | ||
let spyEquals, when, WhenMock, resetAllWhenMocks, verifyAllWhenMocksCalled | ||
let when, WhenMock, resetAllWhenMocks, verifyAllWhenMocksCalled | ||
beforeEach(() => { | ||
spyEquals = jest.spyOn(require('expect/build/jasmineUtils'), 'equals') | ||
when = require('./when').when | ||
@@ -133,33 +131,2 @@ resetAllWhenMocks = require('./when').resetAllWhenMocks | ||
describe('mock implementation', () => { | ||
it('offloads equality check to jasmine equals helper', () => { | ||
const fn = jest.fn() | ||
when(fn).calledWith(1).mockReturnValue('x') | ||
expect(fn(1)).toEqual('x') | ||
expect(spyEquals).toBeCalledWith(1, 1) | ||
expect(fn(2)).toEqual(undefined) | ||
expect(spyEquals).toBeCalledWith(2, 1) | ||
}) | ||
it('works with multiple args', () => { | ||
const fn = jest.fn() | ||
const anyString = expect.any(String) | ||
const myFunction = function () {} | ||
when(fn) | ||
.calledWith(1, 'foo', true, anyString, undefined, myFunction) | ||
.mockReturnValue('x') | ||
expect(fn(1, 'foo', true, 'whatever', undefined, myFunction)).toEqual('x') | ||
expect(spyEquals).toBeCalledWith(1, 1) | ||
expect(spyEquals).toBeCalledWith('foo', 'foo') | ||
expect(spyEquals).toBeCalledWith(true, true) | ||
expect(spyEquals).toBeCalledWith('whatever', anyString) | ||
expect(spyEquals).toBeCalledWith(undefined, undefined) | ||
expect(spyEquals).toBeCalledWith(myFunction, myFunction) | ||
}) | ||
it('only matches exact sets of args, too little or too many args do not trigger mock return', () => { | ||
@@ -166,0 +133,0 @@ const fn = jest.fn() |
48341
-1.62%803
-1.11%