获取语​​法错误:名称参数不是有效的自定义元素名称

发布于 2025-01-15 10:33:10 字数 835 浏览 5 评论 0原文

我正在尝试使用 Enzyme 和 Jest 对我的 React 应用程序进行单元测试,但收到此错误

` FAIL src/components/XYZ/tests/ABC.test.js

● 测试套件无法运行

SyntaxError: Name argument is not a valid custom element name.

测试套件: 2 次失败,共 2 次 测试:总共 0 次 快照: 共 0 个 时间:3.224秒 运行所有测试套件。`

我的 Jest 配置文件如下

"jest": {
    "moduleNameMapper": {
      "d3": "<rootDir>/node_modules/d3/dist/d3.min.js",
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "^.+\\.(css|less|scss)$": "identity-obj-proxy"
    },
    "setupFiles": [
      "./src/"
    ],
    "setupFilesAfterEnv": [
      "./src/setUpTests.js"
    ],
    "testPathIgnorePatterns": [
      "./node_modules/"
    ]
  },

请让我知道我在这里做错了什么? TIA

I am trying to do unit testing of my React app using Enzyme with Jest but getting this error

` FAIL src/components/XYZ/tests/ABC.test.js

● Test suite failed to run

SyntaxError: Name argument is not a valid custom element name.

Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 3.224 s
Ran all test suites.`

My Jest Config file is as following

"jest": {
    "moduleNameMapper": {
      "d3": "<rootDir>/node_modules/d3/dist/d3.min.js",
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)
quot;: "<rootDir>/__mocks__/fileMock.js",
      "^.+\\.(css|less|scss)
quot;: "identity-obj-proxy"
    },
    "setupFiles": [
      "./src/"
    ],
    "setupFilesAfterEnv": [
      "./src/setUpTests.js"
    ],
    "testPathIgnorePatterns": [
      "./node_modules/"
    ]
  },

Please let me know what I am doing wrong here ? TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2025-01-22 10:33:10

我认为这里用于 setupFiles 的配置是错误的。根据笑话文档

https://jestjs.io/docs/configuration#setupfiles-array

setUpFiles 用于“运行一些代码来配置或设置测试环境的模块的路径列表。每个 setupFile 将在每个测试文件中运行一次。由于每个测试都在其自己的环境中运行,因此这些脚本将在这在执行 setupFilesAfterEnv 之前和测试代码本身之前测试环境“

请删除 setUpFiles 或在此处使用正确的文件路径。

I think the configuration used for setupFiles is wrong here. As per jest documentation

https://jestjs.io/docs/configuration#setupfiles-array

setUpFiles is used for "A list of paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment before executing setupFilesAfterEnv and before the test code itself. "

Please either remove setUpFiles or use proper file path here .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文