Redux测试:操作必须是普通对象。使用自定义中间件进行异步操作

Redux测试:操作必须是普通对象。使用自定义中间件进行异步操作,redux,jestjs,enzyme,sinon,Redux,Jestjs,Enzyme,Sinon,我有一个Redux应用程序,它工作正常,没有任何错误。现在我试着用Enzyme,Jest和Sinon测试它: it('calls constructor', () => { sinon.spy(SavedVariantsComponent.prototype, 'constructor') const store = configureStore()(STATE1) wrapper = mount(<SavedVariantsComponent store

我有一个
Redux
应用程序,它工作正常,没有任何错误。现在我试着用
Enzyme
Jest
Sinon
测试它:

  it('calls constructor', () => {
    sinon.spy(SavedVariantsComponent.prototype, 'constructor')
    const store = configureStore()(STATE1)
    wrapper = mount(<SavedVariantsComponent store={store} match={{ params: {} }} />)
    expect(SavedVariantsComponent.prototype.constructor).toHaveProperty('callCount', 1)
  })
loadSavedVariants
看起来是这样的:

export const loadSavedVariants = (combineVariants, familyGuids, variantGuid, tagArray, gene = '') => {
  return (dispatch, getState) => {
...
...
运行
jest
时的错误是:

动作必须是普通对象。使用自定义中间件进行异步操作


这会发出一个在当前情况下可能不起作用的
HTTP请求。如何修复此错误?我需要测试是否调用了构造函数,但稍后还需要查看内部
组件
是如何呈现的,因此需要在那里进行
装载
。我想我在测试中做错了什么,而不是在真正的代码中,因为后者工作时没有任何错误、警告或问题。

您可能需要将模拟存储配置为与redux thunk一起工作。见:


configureStore
看起来像什么?它是否设置了thunk中间件?它只是:
从“redux mock store”导入configureStore
,这可能就是原因所在。你需要设置thunk才能使用thunk中间件。你是说在测试中?在实际的代码中,一切都很完美,没有警告,没有错误。。。如果在测试中,你会如何正确地做到这一点?我留下了一个答案
export const loadSavedVariants = (combineVariants, familyGuids, variantGuid, tagArray, gene = '') => {
  return (dispatch, getState) => {
...
...
import configureStore from 'redux-mock-store'
import thunk from 'redux-thunk'

const middlewares = [thunk] // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares)