Skip to content

Cannot set mock return value on toString #120

@spalladino

Description

@spalladino

The following fails with a TypeError: foo.toString.mockReturnValue is not a function:

interface Foo {
  toString(): string;
}

const foo = mock<Foo>();
foo.toString.mockReturnValue('bar');

My guess for the issue is that mock uses an empty object as a default first argument, which has the toString default js method, and that causes jest-mock-extended not to generate a jest.fn for it.

FWIW, it can be worked around by just providing a different toString to the default implementation:

const foo = mock<Foo>({
  toString: () => 'bar',
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions