When configured to return a function, mockReturnValue calls that function and returns its return value, rather than returning the function itself. This is different to the behaviour of mockReturnValue on a plain jest.fn().
The first assertion succeeds but the second fails:
expect(received).toBe(expected) // Object.is equality
Expected: [Function returnValue]
Received: "hello"
Difference:
Comparing two different types of values. Expected function but received string.
When configured to return a function,
mockReturnValue
calls that function and returns its return value, rather than returning the function itself. This is different to the behaviour ofmockReturnValue
on a plainjest.fn()
.The first assertion succeeds but the second fails:
The problem is here: https://github.com/timkindberg/jest-when/blob/25f0093ea8f845b11864ccc769a653980301f04d/src/when.js#L72
I think the simplest solution is to wrap the
returnValue
in a function here: https://github.com/timkindberg/jest-when/blob/25f0093ea8f845b11864ccc769a653980301f04d/src/when.js#L86-L87