Skip to content

Keep default implementation when attaching to an existing mock #78

@undsoft

Description

@undsoft
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions