使用玩笑时从索引文件导入时出错

我有一个React应用程序,我开始编写测试。我有一个组件,该组件导入其他一些组件,而该组件正在导入其他文件。当它移到constants文件夹时,出现错误。错误看起来像这样:

$ jest
 FAIL  src/components/modals/SidebarDialog/SidebarDialog.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'GET_TASKS_LIST_REQUEST' of undefined

       7 | function dashboard(state = dashboardInitialState,action) {
       8 |     switch (action.type) {
    >  9 |         case dashboardConstants.GET_TASKS_LIST_REQUEST:
         |                                 ^
      10 |             return {
      11 |                 ...state,12 |                 tasksList: {

      at dashboard (src/reducers/dashboard.reducer.js:9:33)

由于某种原因该文件导入未定义:

import { dashboardConstants,dashboardInitialState } from '../constants';

constants/index.js中:

export * from './actions/dashboard.actions';

actions/dashboard.actions.js

export const dashboardConstants...
export const dashboardInitialState...

我可以通过绝对导入来解决它:

import { dashboardConstants,dashboardInitialState } from '../constants/actions/dashboard.actions';

但是有太多这样导入的文件和常量。进口有什么问题? 在开发人员模式下,一切正常。

这是'jest'中的package.json file字段

"jest": {
    "moduleFileExtensions": [
      "js","jsx"
    ],"moduleDirectories": [
      "node_modules","<rootDir>/src"
    ],"moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js"
    },"modulePaths": [
      "<rootDir>"
    ]
  },

upd:我决定为所有文件提供绝对路径,但是当我修复其中一个错误时,该错误并没有消失。所以这不是问题。我不知道我在做什么错。

gengwenshuang 回答:使用玩笑时从索引文件导入时出错

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3167003.html

大家都在问