[英]React testing function calls with Jest and Sinon not working
我试图断言事件后我的React类方法被调用的次数。 我尝试使用sinon.spy和jest.fn()无济于事。
使用sinon.spy:
test('Some test', (done) => {
const page = renderLookupPage();
const formInputButton = page.find('.button').first();
formInputButton.simulate('click');
let spy = sinon.spy(page.instance().myReactMethod);
const button = page.find('.tag').first();
button.simulate('click');
setTimeout(() => {
try {
console.log(spy.callCount); //0
done();
} catch (error) {
done.fail(error);
}
}, 100);
});
与jest.fn():
test('Some test', (done) => {
const page = renderLookupPage();
const formInputButton = page.find('.button').first();
formInputButton.simulate('click');
page.instance().myReactMethod = jest.fn(() => {});
const button = page.find('.tag').first();
button.simulate('click');
setTimeout(() => {
try {
console.log(page.instance().myReactMethod.mock.calls.length); //0
done();
} catch (error) {
done.fail(error);
}
}, 100);
});
我敢肯定,有问题的方法一定会被调用。 更令人困惑的是方法中的console.log语句在
console.log(spy.callCount)
任何正确方向的指针将不胜感激! 干杯!
解决方案很简单,
我搬了线
page.instance()。myReactMethod = jest.fn(()=> {});
在上一行上方:
formInputButton.simulate('click');
不确定为什么这样做有效,并且很高兴听到某人的消息,为什么这会影响结果?
注意:formInputButton不会调用有问题的函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.