开玩笑 moduleNameMapper 查找文件:"resolver":未定义
Jest moduleNameMapper to find files: "resolver": undefined
我在路径下的 component
文件夹中有一个文本文件(除其他外):src/components/text
但是,当使用 webpack 别名 import Text from "components/text";
时,Jest 找不到这个文件。
我尝试添加到 package.json
:
"jest": {
"globals": {
"NODE_ENV": "test"
},
"transform": {
"\.[jt]sx?$": "babel-jest"
},
"verbose": false,
"rootDir": ".",
"collectCoverageFrom": [
"**/*.{js,jsx,ts,tsx}",
"!**/*.d.ts"
],
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx"
],
"moduleNameMapper": {
"\.(css|less|scss|sass|svg)$": "identity-obj-proxy",
"^components/(.*)$": "<rootDir>/src/components/",
"^assets/(.*)$": "<rootDir>/src/assets/",
"^utils/(.*)$": "<rootDir>/src/utils/",
"^styles/(.*)$": "<rootDir>/src/styles/"
"/^locales\/(.*)$/": "<rootDir>/src/locales/",
},
"testMatch": [
"**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"modulePathIgnorePatterns": [
"./dist"
],
"transformIgnorePatterns": [
"/node_modules/(?!(@opt-ui|@equinor))"
],
"coverageDirectory": "<rootDir>/tests/coverage/"
}
但我收到错误消息:
Test suite failed to run
Configuration error:
Could not locate module components/text mapped as:
/Users/olahalvorsen/cssu-dashboard-client/src/components.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^components\/(.*)$/": "/Users/olahalvorsen/cssu-dashboard-client/src/components"
},
"resolver": undefined
}
所以,"^components/(.*)$": "<rootDir>/src/components/"
在 moduleNameMapper 中解决了上面的第一个问题:)
但现在我又遇到了另一个错误:
FAIL src/pages/errorpage/tests/error.test.jsx
● Test suite failed to run
Cannot find module 'locales' from 'src/utils/helpers/helpers.js'
Require stack:
src/utils/helpers/helpers.js
src/components/text/index.jsx
src/pages/errorpage/error.jsx
src/pages/errorpage/tests/error.test.jsx
29 | import { nb, enGB } from "date-fns/locale";
30 |
> 31 | import translations from "locales";
| ^
32 |
33 | export const capitalize = string => {
34 | if (typeof string === "string") {
我更新了上面的package.json。 locales 的相对目录是src/locales
。这不应该工作吗:
"moduleNameMapper": {
"^locales/(.*)$": "<rootDir>/src/locales",
我尝试使用:"/^locales\/(.*)$/": "<rootDir>/src/locales/"
解决方案是使用:"^locales(.*)$": "<rootDir>/src/locales/"
根据日志,我猜您的配置是 /^components\/(.*)$/: "<rootDir>/src/components"
,这与给定的代码 ^components(.*)$
.
不同
假设以上是正确的,那么您可能需要更改为以下内容才能使其正常工作:
{
"moduleNameMapper": {
"/^components\/(.*)$/": "<rootDir>/src/components/" // You missed out `/` before the rest value ``
},
}
检查 fileMock.js
的路径
就我而言,我花了很长时间才意识到 ___test___
文件夹有三个下划线,而不是两个。
并检查文件内容
在我的例子中,这是:module.exports = 'test-file-stub'
您还需要先 install jest-transform-stub。
我在路径下的 component
文件夹中有一个文本文件(除其他外):src/components/text
但是,当使用 webpack 别名 import Text from "components/text";
时,Jest 找不到这个文件。
我尝试添加到 package.json
:
"jest": {
"globals": {
"NODE_ENV": "test"
},
"transform": {
"\.[jt]sx?$": "babel-jest"
},
"verbose": false,
"rootDir": ".",
"collectCoverageFrom": [
"**/*.{js,jsx,ts,tsx}",
"!**/*.d.ts"
],
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx"
],
"moduleNameMapper": {
"\.(css|less|scss|sass|svg)$": "identity-obj-proxy",
"^components/(.*)$": "<rootDir>/src/components/",
"^assets/(.*)$": "<rootDir>/src/assets/",
"^utils/(.*)$": "<rootDir>/src/utils/",
"^styles/(.*)$": "<rootDir>/src/styles/"
"/^locales\/(.*)$/": "<rootDir>/src/locales/",
},
"testMatch": [
"**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"modulePathIgnorePatterns": [
"./dist"
],
"transformIgnorePatterns": [
"/node_modules/(?!(@opt-ui|@equinor))"
],
"coverageDirectory": "<rootDir>/tests/coverage/"
}
但我收到错误消息:
Test suite failed to run
Configuration error:
Could not locate module components/text mapped as:
/Users/olahalvorsen/cssu-dashboard-client/src/components.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^components\/(.*)$/": "/Users/olahalvorsen/cssu-dashboard-client/src/components"
},
"resolver": undefined
}
所以,"^components/(.*)$": "<rootDir>/src/components/"
在 moduleNameMapper 中解决了上面的第一个问题:)
但现在我又遇到了另一个错误:
FAIL src/pages/errorpage/tests/error.test.jsx
● Test suite failed to run
Cannot find module 'locales' from 'src/utils/helpers/helpers.js'
Require stack:
src/utils/helpers/helpers.js
src/components/text/index.jsx
src/pages/errorpage/error.jsx
src/pages/errorpage/tests/error.test.jsx
29 | import { nb, enGB } from "date-fns/locale";
30 |
> 31 | import translations from "locales";
| ^
32 |
33 | export const capitalize = string => {
34 | if (typeof string === "string") {
我更新了上面的package.json。 locales 的相对目录是src/locales
。这不应该工作吗:
"moduleNameMapper": {
"^locales/(.*)$": "<rootDir>/src/locales",
我尝试使用:"/^locales\/(.*)$/": "<rootDir>/src/locales/"
解决方案是使用:"^locales(.*)$": "<rootDir>/src/locales/"
根据日志,我猜您的配置是 /^components\/(.*)$/: "<rootDir>/src/components"
,这与给定的代码 ^components(.*)$
.
假设以上是正确的,那么您可能需要更改为以下内容才能使其正常工作:
{
"moduleNameMapper": {
"/^components\/(.*)$/": "<rootDir>/src/components/" // You missed out `/` before the rest value ``
},
}
检查 fileMock.js
的路径就我而言,我花了很长时间才意识到 ___test___
文件夹有三个下划线,而不是两个。
并检查文件内容
在我的例子中,这是:module.exports = 'test-file-stub'
您还需要先 install jest-transform-stub。