Jest 测试 Redux Saga 无法读取 属性 'then' of undefined on dispatch

Jest test Redux Saga Cannot read property 'then' of undefined on dispatch

我在堆栈溢出中看到了几个类似的条目,但它与我面临的这个问题并不完全相关。我们使用 Jest 进行测试,使用 redux-saga 进行存储。

最近我们试图从 Jasmine 转向 Jest。这个测试曾经对 Jasmine 有效,但对 Jest 却失败了。

这是违规测试,


  describe('componentWillReceiveProps method', () => {
    it('should call dispatch if entityType is changed', (done) => {
      const {wrapper, props} = setup();
      wrapperInstance = wrapper.instance();
      wrapperInstance.isPagePoppedFromHistory = false;

      setTimeout(() => {
        expect(props.dispatch).toHaveBeenCalled();
        done();
      }, 300);
      wrapperInstance.UNSAFE_componentWillReceiveProps(Object.assign({}, props, {
        entityType: {key: ENTITY_TYPE_HCO}
      }));
    });
  });

收到的错误是

Error: Uncaught [TypeError: Cannot read property 'then' of undefined]
        at reportException (node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
        at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:645:7)
        at ontimeout (timers.js:498:11)
        at tryOnTimeout (timers.js:323:5)
        at Timer.listOnTimeout (timers.js:290:5) TypeError: Cannot read property 'then' of undefined
        at UNSAFE_componentWillReceiveProps.setTimeout (/index.js:294:11)
        at Timeout.callback [as _onTimeout] (/Window.js:643:19)
        at ontimeout (timers.js:498:11)
        at tryOnTimeout (timers.js:323:5)
        at Timer.listOnTimeout (timers.js:290:5)

组件违规代码行是这些

componentWillReceiveProps(){
 ...
 setTimeout(() => {
          dispatch(fetchTableResults(resultsLimit, filterParams, qString, currentOrderOption,
            undefined, undefined, resultBounds))
            .then(() => {
              dispatch(changePageNumber(nextPage));
            });
        });

 ...
}

知道我们为什么会看到这个吗?任何帮助将不胜感激。

按照 xdeepakv 的建议结束了模拟调度方法。