timkindberg / jest-when

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

Keep original function implementation when not matched #82

Closed materkel closed 3 years ago

materkel commented 3 years ago

resolves #78

imo this is also more in line with the jest default behavior

timkindberg commented 3 years ago

Thank you very much for this contribution!

Do you know will this also work with a mock that has an existing mockReturnValue as well?

See #59

materkel commented 3 years ago

Just checked the test setup @narthur provided in #59 with my solution, unfortunately this does not work, the original jest setup still has to be defined before calling when on it in this case.

const fn = jest.fn()

when(fn).calledWith(1).mockReturnValue('a')

fn.mockReturnValue('b')

expect(fn(1)).toEqual('a') // fails and will still return 'b' (as before my change)
expect(fn(2)).toEqual('b')

Not sure if #59 is solved quite as easily (from my limited understanding of the code)