Skip to content

Commit

Permalink
refactor: update predicate function name
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Jul 11, 2024
1 parent 4a60233 commit e6368b3
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,20 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
/**
* Used for `toHaveBeenCalledBefore` and `toHaveBeenCalledAfter` to determine if the expected spy was called before the result spy.
*/
function predicate(expectInvocationCallOrder: number[], resultInvocationCallOrder: number[], failIfNoFirstInvocation: number): boolean {
if (expectInvocationCallOrder.length === 0) {
function isSpyCalledBeforeAnotherSpy(beforeSpy: MockInstance, afterSpy: MockInstance, failIfNoFirstInvocation: number): boolean {
const beforeInvocationCallOrder = beforeSpy.mock.invocationCallOrder

const afterInvocationCallOrder = afterSpy.mock.invocationCallOrder

if (beforeInvocationCallOrder.length === 0) {
return !failIfNoFirstInvocation
}

if (resultInvocationCallOrder.length === 0) {
if (afterInvocationCallOrder.length === 0) {
return false
}

return expectInvocationCallOrder[0] < resultInvocationCallOrder[0]
return beforeInvocationCallOrder[0] < afterInvocationCallOrder[0]
}

def(
Expand All @@ -726,16 +730,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
}

if (!isMockFunction(expectSpy)) {
throw new TypeError(
`${utils.inspect(expectSpy)} is not a spy or a call to a spy`,
)
}

this.assert(
predicate(
expectSpy.mock.invocationCallOrder,
resultSpy.mock.invocationCallOrder,
isSpyCalledBeforeAnotherSpy(
expectSpy,
resultSpy,
failIfNoFirstInvocation,
),
`expected "${expectSpy.getMockName()}" to have been called before "${resultSpy.getMockName()}"`,
Expand All @@ -756,16 +754,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
}

if (!isMockFunction(expectSpy)) {
throw new TypeError(
`${utils.inspect(expectSpy)} is not a spy or a call to a spy`,
)
}

this.assert(
predicate(
resultSpy.mock.invocationCallOrder,
expectSpy.mock.invocationCallOrder,
isSpyCalledBeforeAnotherSpy(
resultSpy,
expectSpy,
failIfNoFirstInvocation,
),
`expected "${expectSpy.getMockName()}" to have been called after "${resultSpy.getMockName()}"`,
Expand Down

0 comments on commit e6368b3

Please sign in to comment.