问题
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