search

Home  >  Q&A  >  body text

Why does jest.mock return a mock object with default properties?

I'm trying to mock an object like this:

export default person = {
   firstName: "xxx",
   LastName: "xxx",
   getFullName: () => this.firstName + this.LastName
}

jest.mock('../person', () => ({
  getFullName: () => "John Smith"
}));

So I just want to mock the getFullName method, but when I run jest, I find that person is mocked as:

{
   default: { getFullName: () => "John Smith" }
   ...
}

How can I get rid of the "default" properties that I only want:

{
   getFullName: () => "John Smith"
}

P粉547420474P粉547420474504 days ago769

reply all(1)I'll reply

  • P粉041856955

    P粉0418569552023-09-16 21:48:22

    You can replace mock with spyOn method.

    jest.spyOn(person, 'getFullName').mockImplementation(() => "John Smith");

    reply
    0
  • Cancelreply