【问题标题】:Gatsby + Jest - How to enable support for the experimental syntax 'jsx'?Gatsby + Jest - 如何启用对实验语法“jsx”的支持?
【发布时间】:2021-11-04 07:11:38
【问题描述】:

遵循 Gatsby 单元测试文档: https://www.gatsbyjs.com/docs/how-to/testing/unit-testing/#writing-tests

我收到了这个错误:

详情:

SyntaxError: ../Projects/gatsby/testing-first-steps/src/components/__tests__/header.js: Support for the experimental syntax 'jsx' isn't currently enabled (9:15):
       7 |   it("renders correctly", () => {
       8 |     const tree = renderer
    >  9 |       .create(<Header siteTitle="Default Starter" />)
         |               ^
      10 |       .toJSON()
      11 |     expect(tree).toMatchSnapshot()
      12 |   })
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.

【问题讨论】:

    标签: jestjs babeljs gatsby


    【解决方案1】:

    解决了! ? https://www.gatsbyjs.com/docs/how-to/custom-configuration/babel/#how-to-use-a-custom-babelrc-file

    添加这个:

    npm install --save-dev babel-preset-gatsby
    

    然后在.babelrc 文件中添加:

    {
      "presets": [
        [
          "babel-preset-gatsby",
          {
            "targets": {
              "browsers": [">0.25%", "not dead"]
            }
          }
        ]
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2020-11-10
      • 2021-05-17
      • 2021-04-11
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      相关资源
      最近更新 更多