我想模拟一个名为 Dog 的构造函数
Dog = jest.fn(()=>{
return{
name:"spike",
bark:function(){
return "bhow " +this.name;
}
}
})
function foo(){
const d = new Dog();
return d.bark();
}
test("testing foo",()=>{
const result = foo();
expect(Dog).toHaveBeenCalledTimes(1);
expect(result).toBe("bhow spike");
expect(Dog.mock.instances.length).toBe(1);
expect(Dog.mock.instances[0].name).toBe("spike");
//this test failed with expected spike received undefined
});
但是expect(Dog.mock.instances[0].name).toBe("spike");
收到未定义的预期峰值的失败
开玩笑版本 24.8.0 节点版本 10.15.0