【问题标题】:Enzyme setup error: "Jest encountered an unexpected token"酶设置错误:“Jest 遇到了意外的令牌”
【发布时间】:2020-04-06 01:30:36
【问题描述】:

我在使用 react-16 作为前端的项目中设置酶时遇到问题

我在尝试测试时遇到此错误

这是我的 package.json

"devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.7.4",
    "@babel/preset-react": "^7.0.0",
    "axios": "^0.19",
    "babel-plugin-syntax-class-properties": "^6.13.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "cross-env": "^5.1",
    "react": "^16.2.0"
},
"dependencies": {
    "@babel/core": "^7.6.4",
    "@babel/preset-env": "^7.6.3",
    "babel-cli": "^6.26.0",
    "babel-jest": "^24.9.0",
    "babel-loader": "^8.0.6",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.15.1",
    "jest": "^24.9.0",
    "raf": "^3.4.1",
    "react-bootstrap": "^1.0.0-beta.14",
    "react-dates": "^21.5.0",
    "react-dom": "^16.11.0",
    "react-modal": "^3.11.1",
    "react-redux": "^7.1.3",
    "react-router-dom": "^5.1.2",
    "react-test-renderer": "^16.12.0",
    "react-toolbox": "^2.0.0-beta.13",
    "redux": "^4.0.4",
  }

测试配置文件test.config.json

{
    "testRegex": "((\\.|/*.)(test))\\.js?$",
    "setupFilesAfterEnv": [
        "<rootDir>/app/resources/js/test/setupTests.js"
    ]
}

Setut 测试配置文件 setupTests.js

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({
    adapter: new Adapter()
});

【问题讨论】:

  • 你试过解构配置吗?我不认为它在默认导出中
  • 是的,我尝试了同样的错误,我认为问题出在其中一个包上,但我真的不知道是哪个包

标签: reactjs jestjs babeljs enzyme react-16


【解决方案1】:

我发现了我的问题。我重新安装了这个包

npm install @babel/core babel-loader @babel/preset-env @babel/preset-react @babel/plugin-proposal-class-properties @babel/plugin-proposal-object-rest-spread

为了测试,我使用了 PhpStorm,所以我必须将配置文件添加到 Jest options

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多