【发布时间】:2019-05-07 05:51:54
【问题描述】:
我想模拟一个名为 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
【问题讨论】: