酶设置错误:“ Jest遇到意外令牌”

问题描述 投票:0回答:1

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

尝试测试时出现此错误

enter image description here

这是我的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 babel enzyme react-16
1个回答
-1
投票

Jest希望文件在某些​​软件包使用ESM模块时处于CJS格式,因此无法解析错误。

transformIgnorePatterns: ["/node_modules/(?!package-name)"],

确保包名将被jest转换,将以下内容添加到jest配置中:

transformIgnorePatterns: ["/node_modules/(?!package-name)"],

这意味着:“忽略node_modules中除软件包名称之外的所有内容。

在您的情况下是酶

© www.soinside.com 2019 - 2024. All rights reserved.