如何解决React JS Jest模拟静态方法,但不能两次调用该方法
我正在编写一个玩笑测试来两次调用静态方法。确实会第一次调用静态方法,但不会第二次通过console.log()验证。
我不知道为什么它不会第二次调用。 我尝试了jest.clearAllMocks(),jest.mockReset(),但这只是清除了模拟功能。由于某些奇怪的原因,该组件将不会进行第二次调用。
const Component = require('./myFile.jsx') .default
it('',() => {
jest.mock('./myfile.jsx')
const mockFn = jest.fn()
mockFn.mockReturnValue(true)
Component.setValue('myValue')
Component.setValue = mockFn
let result = Component.SetValue('myValue')
expect(result).toEqual(true)
// so far so good,this works,console.log see 'myValue' is passed to the static method
Component.setValue('')
Component.setValue = mockFn
let result = Component.SetValue('')
expect(result).toEqual(true)
// the SetValue is never call for a 2nd time. stuck here.
}
In my jsx code
static SetValue(Name) {
console.log('----> Name: ',Name)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。