Babel.js 在Jest测试期间出现“不支持的jsx语法”错误

gr8qqesn  于 2022-12-08  发布在  Babel
关注(0)|答案(2)|浏览(347)

使用SVG作为ReactComponent,在Jest测试期间出现以下错误:

SyntaxError: corner-resize-arrow.svg: Support for the experimental syntax 'jsx' isn't currently enabled (1:1):
<svg width="12" height="12" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg'
    <path d=-"M12 8.58307e-05L3.05176e-05 12L3.1209e-05 9L9
      8.51393e-05L12 8.58307e-05Z" fill="white"/>
    <path d="M6 5.34058e-05L-4.76837e-06 5.99988L-4.5538e-06 2.99988L3
      5.31912e-05L6 5.34058e-05z" fill="white"/>
</svg>
Add @babel/preset-react (https://git.io/IfeDR) to the 'presets' section of your
Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-isx
(https://git.io/vb4yA) to the 'plugins' section to enable parsing,

但是我的配置文件中已经有了@babel/preset-react。

module.exports = {
    "presets": ['@babel/preset-env', '@babel/preset-react'],
    "plugins": ['@babel/plugin-proposal-class-properties']
}

下面是pckage.json文件中的jest配置。

"jest": {
    "verbose": true,
    "collectCoverage": true,
    "collectCoverageFrom":[
        "**/src/**"
        "!**/node_modules/**",
        "!**/src/index**",
        "!**/src/const**"
        "!**/src/Redux/constants**"
        "!**/coverage/**"
        "!**/public/**"
        "!**/tests/**"
        "!**/webpack/**",
        "!**/babel.config.js**"
        "!**/src/serviceWorker.js**"
    ],
    "coverageReporters": [
        "lcov"
        "text"
        "clover"
        "json"
    ],
    "coverageDirectory":"./coverage",
    "setupFiles": [
        "./src/setupTests.js"
    ],
    "moduleFileExtensions": [
        "js",
        "jsx"
    ],
    "moduleNameMapper": {
         "'\(ess|less|scss|sass)$": "identity-obj-proxy"
    },
    "testResultsProcessor":"jest-sonar-reporter"
}

注:NPM启动命令工作正常。

mf98qq94

mf98qq941#

您的babel配置中的presets缺少preset-env条目的@

module.exports = {
    "presets": ['@babel/preset-env', '@babel/preset-react'],
    "plugins": ['@babel/plugin-proposal-class-properties']
}
4ktjp1zp

4ktjp1zp2#

如果你的项目中没有安装babel-jest,你需要添加它。如果你使用NPM作为你的包管理器,npm install --save-dev babel-jest可以完成这个任务。
Jest会自动检测并使用它。

相关问题