如何为反应和笑话测试框架配置.babelrc?

我从头开始创建了一个React应用(createapp.dev),并且正在使用包裹捆绑器对其进行捆绑。

我尝试使用https://testing-library.com/和玩笑对它进行单元测试,但我一直收到此错误。

src/components/App.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse,e.g. it's not plain JavaScript.

    By default,if Jest sees a Babel config,it will use that
to transform your files,ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed,you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

如何解决? 这些是我的文件 .babelrc

{
    "presets": [
      [
        "@babel/preset-env",{
          "modules": false
        }
      ],["@babel/preset-react"]
    ],"plugins": ["@babel/plugin-proposal-class-properties" ]
  }

package.json

{

  "main": "src/index.js","scripts": {
    "test": "jest","start": "parcel public/index.html --host 127.0.0.1 --port 8080",},"dependencies": {
    "react": "^16.12.0","react-dom": "^16.12.0"
  },"devDependencies": {
    "@babel/core": "^7.8.7","@babel/plugin-proposal-class-properties": "^7.8.3","@babel/preset-env": "^7.8.7","@babel/preset-react": "^7.8.3","@testing-library/react": "^10.0.1","babel-jest": "^25.1.0","jest": "^25.1.0","parcel": "^1.12.4","parcel-bundler": "^1.12.4","prettier": "^1.19.1"
  }
}
jianggang158 回答:如何为反应和笑话测试框架配置.babelrc?

我意识到安装@testing-library/reactjest不够,我添加了以下内容

@testing-library/dom
@testing-library/jest-dom
@testing-library/user-event
babel-jest 

我也将.babelrc更改为这样

{
    "presets": [
      [
        "@babel/preset-env",{
          "targets": {
            "node": "current"
          }
        }
      ],["@babel/preset-react"]
    ],"plugins": ["@babel/plugin-proposal-class-properties" ]
  }
本文链接:https://www.f2er.com/2647337.html

大家都在问