Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Jest在导入具有绝对路径的组件时给出“找不到模块”_Node.js_Reactjs_Npm_Jestjs - Fatal编程技术网

Node.js Jest在导入具有绝对路径的组件时给出“找不到模块”

Node.js Jest在导入具有绝对路径的组件时给出“找不到模块”,node.js,reactjs,npm,jestjs,Node.js,Reactjs,Npm,Jestjs,运行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) package.json "jest": { "collectCoverageFrom": [

运行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)
package.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"
    ]
  },
“开玩笑”:{
“CollectionCoverage from”:[
“src/***.{js,jsx,mjs}”
],
“设置文件”:[
“/config/polyfills.js”
],
“测试匹配”:[
“/src/***/\uuuuuuuu测试{js,jsx,mjs}”,
“/src/**/?(*)(规范测试)。{js,jsx,mjs}”
],
“测试环境”:“节点”,
“testURL”:”http://localhost",
“转变”:{
“^.+\.(js|jsx|mjs)$”:“/node_modules/babel jest”,
“^.+\.css$”:“/config/jest/cstransform.js”,
“^(?。*\(js | jsx | mjs | css | json)$)”:“/config/jest/fileTransform.js”
},
“transformIgnorePatterns”:[
“[/\\\\\]节点单元模块[/\\\\].+\\.(js | jsx | mjs)$”
],
“模块目录”:[
“节点_模块”,
“src”
],
“moduleNameMapper”:{
“^react native$”:“react native web”
},
“moduleFileExtensions”:[
“web.js”,
“js”,
“json”,
“web.jsx”,
“jsx”,
“节点”,
“mjs”
]
},
运行仅在树中包含相对路径的文件的测试可以正确运行


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

因为在
package.json
中,您有:

"moduleDirectories": [
    "node_modules",
    "src"
]
这表示您导入的每个模块将首先被查找到
node\u modules
,如果找不到,将被查找到
src
目录

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

import AppContainer from 'views/app';
请注意,此路径是
src
目录的绝对路径,您不必导航将其定位为相对路径


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

我想您正在寻找:或者

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

我会确保在根数组中包含
,在modulePath数组中包含
,在moduleDirectory数组中包含
节点模块
,除非您有充分的理由排除它们

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}
“开玩笑”:{
“根”:[
"",
“/home/some/path/”
],
“模块路径”:[
"",
“/home/some/other/path”
],
“模块目录”:[
“节点_模块”
],
}
添加

"moduleDirectories": [
    "node_modules",
    "src"
]
如果在package.json文件中有Jest的配置,则应该可以工作

如果您有一个
jest.config.js
文件,您应该将其添加到那里,否则package.json将被此配置文件覆盖(并忽略)。因此,在您的
jest.config.js
文件中:

module.exports = {
// ... lots of props
  moduleDirectories: ["node_modules", "src"],
// ...
}

添加
\uu esModule:true
为我解决了这个问题

jest.mock('module',()=>({
  __esModule: true,                    // this makes it work
  default: jest.fn()
}));

希望这对别人有帮助。尽管这不是一个非常具体的问题。

尝试从“/src/views/app”导入AppContainer我需要知道如何运行绝对路径,这样我就不必在导入时后退多个目录,或者在移动文件时更新尽可能多的文件
jest.mock('module',()=>({
  __esModule: true,                    // this makes it work
  default: jest.fn()
}));