this.firstName+this.LastName}jest.mock('../person', ()=>({getFullName:()=>"JohnSmith">
我試著模擬一個對象,如下所示:
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" }
您可以將mock替換為spyOn方法。
#jest.spyOn(person, 'getFullName').mockImplementation(() => "約翰史密斯");