最佳答案
运行 Jest 时收到以下错误
Cannot find module 'src/views/app' from 'index.jsx'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
at Object. (src/index.jsx:4:12)
Index.jsx
import AppContainer from 'src/views/app';
包裹 Json
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx,mjs}"
],
"setupFiles": [
"/config/polyfills.js"
],
"testMatch": [
"/src/**/__tests__/**/*.{js,jsx,mjs}",
"/src/**/?(*.)(spec|test).{js,jsx,mjs}"
],
"testEnvironment": "node",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js|jsx|mjs)$": "/node_modules/babel-jest",
"^.+\\.css$": "/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|mjs|css|json)$)": "/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"
]
},
运行只包含树中相对路径的文件的测试正确运行。
现在我正在寻找如何配置 Jest 不会在绝对路径上失败的解决方法。