gpt4 book ai didi

javascript - 开 Jest moduleNameMapper 查找文件 : "resolver": undefined

转载 作者:行者123 更新时间:2023-12-04 12:23:20 24 4
gpt4 key购买 nike

我在 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/$1",
"^assets/(.*)$": "<rootDir>/src/assets/$1",
"^utils/(.*)$": "<rootDir>/src/utils/$1",
"^styles/(.*)$": "<rootDir>/src/styles/$1"
"/^locales\/(.*)$/": "<rootDir>/src/locales/$1",
},
"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$1.

Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^components\/(.*)$/": "/Users/olahalvorsen/cssu-dashboard-client/src/components$1"
},
"resolver": undefined
}
所以, "^components/(.*)$": "<rootDir>/src/components/$1"在 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$1",
我尝试使用: "/^locales\/(.*)$/": "<rootDir>/src/locales/$1"解决方案是使用: "^locales(.*)$": "<rootDir>/src/locales/$1"

最佳答案

根据日志,我猜您的配置是 /^components\/(.*)$/: "<rootDir>/src/components$1"与您给定的代码不同的是 ^components(.*)$ .
假设以上是正确的,因此您可能需要更改为以下内容才能使其正常工作:

{
"moduleNameMapper": {
"/^components\/(.*)$/": "<rootDir>/src/components/$1" // You missed out `/` before the rest value `$1`
},
}

关于javascript - 开 Jest moduleNameMapper 查找文件 : "resolver": undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64816788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com