“没有为键X提供减速器". Redux开玩笑测试中的console.error [英] "No reducer provided for key X" console.error in redux jest test

查看:48
本文介绍了“没有为键X提供减速器". Redux开玩笑测试中的console.error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
   ...
});

奇怪的是,没有代码实际上是从索引调用根减少器的.我的测试直接将切片称为"newLineItemReducer".似乎仅仅是从reducer/index.ts导入共享接口和大小写功能的行为引起了问题(如果我删除combineReducer导出就没有错误)?

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)?

推荐答案

我遇到了同样的问题,正如上面@ michael-radionov在评论部分中提到的,这是循环导入,并且我使用了文章

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

这篇关于“没有为键X提供减速器". Redux开玩笑测试中的console.error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆