Why does jest.mock return a mock object with default properties?
P粉547420474
P粉547420474 2023-09-16 00:25:43
0
1
685

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粉547420474
P粉547420474

reply all(1)
P粉041856955

You can replace mock with spyOn method.

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!