Skip to content

Commit

Permalink
Access the equals util by grabbing it from the expect this arg. T…
Browse files Browse the repository at this point in the history
…emporary hack until core jest exposes this util officially.
  • Loading branch information
timkindberg committed Oct 5, 2021
1 parent b299e3c commit 5a8cc93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
24 changes: 20 additions & 4 deletions src/when.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
const assert = require('assert')
const utils = require('expect/build/jasmineUtils')

let registry = new Set()

const getCallLine = () => (new Error()).stack.split('\n')[4]

/**
* 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) => {
// Propagate failure to the end
if (!match) {
Expand All @@ -27,10 +43,10 @@ const checkArgumentMatchers = (expectCall, args) => (match, matcher, i) => {
}

if (isFunctionMatcher) {
return matcher(arg, utils.equals)
return matcher(arg, equals)
}

return utils.equals(arg, matcher)
return equals(arg, matcher)
}

const NO_CALLED_WITH_YET = Symbol('NO_CALLED_WITH')
Expand All @@ -53,7 +69,7 @@ class WhenMock {
// * call mocks with equal matchers are removed
// * `once` mocks are used prioritized
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() })
.sort((a, b) => {
// Once mocks should appear before the rest
Expand Down
35 changes: 1 addition & 34 deletions src/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ const errMsg = ({ expect, actual }) =>
new RegExp(`Expected.*${expect}.*\\nReceived.*${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
resetAllWhenMocks = require('./when').resetAllWhenMocks
verifyAllWhenMocksCalled = require('./when').verifyAllWhenMocksCalled
Expand Down Expand Up @@ -129,37 +127,6 @@ describe('When', () => {
})

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', () => {
const fn = jest.fn()

Expand Down

0 comments on commit 5a8cc93

Please sign in to comment.