"没有为关键X"提供减速器; redux jest测试中的console.error

时间:2017-11-22 18:49:19

标签: typescript redux jest

我有一个正在通过的减速器测试,但最后抛出了这个奇怪的错误:

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

的src /减速器/ 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 /减速器/ 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;

测试/ newLineItemReducer.spec.ts

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

奇怪的是,没有代码实际上是从索引中调用root reducer。我的测试是直接调用切片&#34; newLineItemReducer&#34;。似乎只是从reducer/index.ts导入共享接口和案例函数的行为导致问题(如果我删除combineReducer导出没有错误)?

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,正如上面@ michael-radionov在评论部分中提到的那样,这是循环导入,并且我使用circular-dependency-plugin插件来跟踪这些循环导入,可以找到有关插件使用的更多信息在插件存储库中或以下article