Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jest模拟或测试异步cb函数?_Javascript_Reactjs_Mocking_Jestjs - Fatal编程技术网

Javascript 如何使用jest模拟或测试异步cb函数?

Javascript 如何使用jest模拟或测试异步cb函数?,javascript,reactjs,mocking,jestjs,Javascript,Reactjs,Mocking,Jestjs,假设我有这个: componentDidMount() { window.Something.windowFunction(EVENT_HANDLER, this.myFunc) } myFunc() { // test that this was called } 如何模拟窗口对象以确保进入myFunc 我已经做到了 const mockFn = jest.fn().mockImplementation((eventHandler, eventCallback) => {

假设我有这个:

componentDidMount() {
 window.Something.windowFunction(EVENT_HANDLER, this.myFunc)
}

myFunc() {
  // test that this was called
}
如何模拟窗口对象以确保进入
myFunc

我已经做到了

const mockFn = jest.fn().mockImplementation((eventHandler, eventCallback) => {
    return eventCallback()
})

global.Something = {
    windowFunction: mockFn('event-handler', () => jest.fn()),
}
但它从不调用
myFunc