Как мне настроить .babelr c для фреймворка реагирования и шутки? - PullRequest
0 голосов
/ 14 марта 2020

Я создал приложение реакции с нуля ( 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

Как я могу это исправить? Это мои файлы .babelr c

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

пакет. 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"
  }
}

1 Ответ

0 голосов
/ 16 марта 2020

Я понял, что установки @testing-library/react и jest недостаточно, я добавил следующее

@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" ]
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...