搜索

首页  >  问答  >  正文

为什么 jest.mock 返回具有默认属性的模拟对象?

我试图模拟一个对象,如下所示:

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

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

所以我只想模拟 getFullName 方法,但是当我运行 jest 时,我发现 person 被模拟为:

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

我怎样才能摆脱我只想要的“默认”属性:

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

P粉547420474P粉547420474505 天前770

全部回复(1)我来回复

  • P粉041856955

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

    您可以将mock替换为spyOn方法。

    jest.spyOn(person, 'getFullName').mockImplementation(() => "约翰·史密斯");

    回复
    0
  • 取消回复