使用Jest监视componentDidMount中的方法调用

使用Jest监视componentDidMount中的方法调用,第1张

使用Jest监视componentDidMount中的方法调用

关键是使用笑话

spyOn
方法。应该是这样的:

const spy = jest.spyOn(Component.prototype, 'methodName');const wrapper = mount(<Component {...props} />);wrapper.instance().methodName();expect(spy).toHaveBeenCalled();

如此处所示:测试功能是否被称为反应和酶

请注意 ,最好的做法是在每次测试运行后清除监视功能

let spyafterEach(() => {  spy.mockClear()})

https://facebook.github.io/jest/docs/en/jest-
object.html#jestclearallmocks



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5082834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存