const test = jest.fn((yes: boolean): string => (yes ? 'pizza' : 'pasta'));
when(test).calledWith(true).mockReturnValueOnce('Luigi');
console.log(test(true));
console.log(test(false));
// Actual:
// Luigi
// undefined
// Expected
// Luigi
// pasta
Rationale:
I have a factory in one place of a system that returns a method that is already mocked in certain way. What I was specifically looking for is a convenient way of adding another condition to this mock in a different part of the system.
I understand that I can rewrite my original test declaration using jest-when, but the current behaviour of overriding existing implementations seems intrusive.
The syntax of when().calledWith() itself implies behaviour that only happens under specific conditions.
Rationale:
I have a factory in one place of a system that returns a method that is already mocked in certain way. What I was specifically looking for is a convenient way of adding another condition to this mock in a different part of the system.
I understand that I can rewrite my original
testdeclaration using jest-when, but the current behaviour of overriding existing implementations seems intrusive.The syntax of
when().calledWith()itself implies behaviour that only happens under specific conditions.