node.js 使用绝对路径导入组件时,Jest 给出“无法找到模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50863312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:50:39  来源:igfitidea点击:

Jest gives `Cannot find module` when importing components with absolute paths

node.jsreactjsnpmjestjs

提问by Seth McClaine

Receiving the following error when running Jest

运行 Jest 时收到以下错误

Cannot find module 'src/views/app' from 'index.jsx'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
  at Object.<anonymous> (src/index.jsx:4:12)

index.jsx

索引.jsx

import AppContainer from 'src/views/app';

package.json

包.json

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
    ],
    "testEnvironment": "node",
    "testURL": "http://localhost",
    "transform": {
      "^.+\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
      "^.+\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\]node_modules[/\\].+\.(js|jsx|mjs)$"
    ],
    "moduleDirectories": [
        "node_modules",
        "src"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs"
    ]
  },

My tests that run files that only contain relative paths in the tree run correctly.

我运行仅包含树中相对路径的文件的测试正确运行。

To Clarify, I'm looking for how to configure Jest to not fail on absolute paths.

为了澄清,我正在寻找如何配置 Jest 在绝对路径上不会失败。

采纳答案by Hriday Modi

Since in package.jsonyou have:

因为package.json你有:

"moduleDirectories": [
    "node_modules",
    "src"
]

Which says that each module you import will be looked into node_modulesfirst and if not found will be looked into srcdirectory.

这表示您导入的每个模块将node_modules首先被查看,如果没有找到,将被查看到src目录中。

Since it's looking into srcdirectory you should use:

由于它正在查看src目录,因此您应该使用:

import AppContainer from 'views/app';

Please note that this path is absolute to the srcdirectory, you do not have to navigate to locate it as relative path.

请注意,此路径是src目录的绝对路径,您无需导航以将其定位为相对路径。

OR you can configure your root directory in moduleDirectories inside your pakcage.json so that all your components could be imported as you want it.

或者,您可以在 pakcage.json 中的 moduleDirectories 中配置根目录,以便可以根据需要导入所有组件。

回答by twils0

I think you're looking for: rootsor modulePathsand moduleDirectories

我认为您正在寻找:rootsmodulePathsmoduleDirectories

You can add both relative and absolute paths.

您可以添加相对路径和绝对路径。

I would make sure to include <rootDir>in the roots array, <rootDir>in the modulePaths array, and node_modulesin the moduleDirectories array, unless you've got a good reason to exclude them.

我会确保包含<rootDir><rootDir>root 数组、modulePaths 数组和node_modulesmoduleDirectories 数组中,除非您有充分的理由排除它们。

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}

回答by u445908

I also met this problem, when I was adding "jest" support for an legacy (2.5.x) vue project.

当我为遗留(2.5.x)vue 项目添加“jest”支持时,我也遇到了这个问题。

I googled and found it seems that upgrading to babel-7 should solve this problem. but finally, I re-create a latest version vue project. and it OK now.

我用谷歌搜索,发现升级到 babel-7 似乎应该可以解决这个问题。但最后,我重新创建了一个最新版本的 vue 项目。现在好了。