“No reducer provided for key X” console.error in redux jest test

£可爱£侵袭症+ 提交于 2020-07-09 03:56:27

问题


I have a reducer test that is passing but throwing out this weird error at the end:

console.error node_modules/redux/lib/utils/warning.js:14
    No reducer provided for key "newProducts"

src/reducer/index.ts

import newLineItemReducer from "./newLineItemReducer";
import renewedLineItemReducer from "./renewedLineItemReducer";

export interface LineItemState{
  renewedProducts: LineItem[]
  newProducts: LineItem[]
}

//used by both reducers
export interface LineItem{ 
  ...
}

// used by both "new" and "renewed" slice reducers
export function sharedFunction1() {
  ...
}

export default combineReducers<LineItemState>({
  renewedProducts: renewedLineItemReducer,
  newProducts: newLineItemReducer
});

src/reducer/newLineItemReducer.ts

import {LineItem, sharedFunction1 } from "./";

type Action = ...;
const newLineItemReducer =
  (state: LineItem[] = [], action: Action): LineItem[] => {
  switch (action.type) {
    case ...:
      sharedFunction1(state, action.foo);
  }
}
export default newLineItemReducer;

test/newLineItemReducer.spec.ts

import newLineItemReducer from "@src/reducers/newLineItemReducer";
test("foo", () => {
   let state = //
   let action = //
   const updatedState = newLineItemReducer(state, action);
   ...
});

The weird thing is there is no code that is actually calling the root reducer from index. My test is directly calling the slice "newLineItemReducer". Seems like just the act of importing the shared interface and case function from reducer/index.ts is causing the issue (If I remove combineReducer export there is no error)?


回答1:


I had the same issue and As mentioned by @michael-radionov above in the comments section this was a circular import, and I utilized circular-dependency-plugin plugin to track these circular imports, more info about plugin usage can be found on the plugin repo or in the following article



来源:https://stackoverflow.com/questions/47441905/no-reducer-provided-for-key-x-console-error-in-redux-jest-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!