我试图模拟一个对象,如下所示:
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"
} Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以将mock替换为spyOn方法。
jest.spyOn(person, 'getFullName').mockImplementation(() => "约翰·史密斯");