timkindberg / jest-when

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

2.8 -> 3.2 mocking does not work on short-form abc.map(makeFoo) #65

Closed mcbain closed 3 years ago

mcbain commented 3 years ago
import {makeFoo} from "foo"
import {when} from "jest-when"
when(makeFoo)
    .calledWith('bar')
    .mockReturnValue(123)

abc.map(makeFoo)              // mocking does not work
abc.map(x => makeFoo(x)) //  mocking works

Any idea why the short form mocking does not work with 3.2 ?

timkindberg commented 3 years ago

Did you figure it out?

mcbain commented 3 years ago

No, but no time to really dig into it. We end up using the longer form which is even more readable :)

timkindberg commented 3 years ago

Figured it out... it's because calledWith needs to be explicit since v3, you must define all args passed to the function. Partial definitions of args are no longer supported. And when you map the function gets passed value, index, array, not just the value. So it wasn't a match.

You would have to change your code a bit:

when(makeFoo)
    .calledWith('bar', expect.anything(), expect.anything())
    .mockReturnValue(123)