React 应用程序:Jest 遇到意外令牌

我尝试测试我通过create-react-app. 只有一个自动生成的测试文件:


import React from 'react';

import ReactDOM from 'react-dom';

import App from './App';


it('renders without crashing', () => {

  const div = document.createElement('div');

  ReactDOM.render(<App />, div);

  ReactDOM.unmountComponentAtNode(div);

});

这是我的package.json(我也有 package-lock.json(约 15k 行)):


   {

      "name": "project",

      "version": "0.1.0",

      "private": true,

      "dependencies": {

        "avro-js": "^1.9.1",

        "axios": "^0.19.0",

        "bootstrap": "^4.3.1",

        "jquery": "^3.4.1",

        "prop-types": "^15.7.2",

        "react": "^16.10.2",

        "react-dom": "^16.10.2",

        "react-redux": "^7.1.1",

        "react-scripts": "3.2.0",

        "react-syntax-highlighter": "^11.0.2",

        "reactstrap": "^8.0.1",

        "redux": "^4.0.4",

        "redux-thunk": "^2.3.0"

      },

      "scripts": {

        "start": "react-scripts start",

        "build": "react-scripts build",

        "test": "react-scripts test",

        "eject": "react-scripts eject"

      },

      "eslintConfig": {

        "extends": "react-app"

      },

      "browserslist": {

        "production": [

          ">0.2%",

          "not dead",

          "not op_mini all"

        ],

        "development": [

          "last 1 chrome version",

          "last 1 firefox version",

          "last 1 safari version"

        ]

      }

    }

当我尝试使用测试脚本对其进行测试时,出现此错误:


Jest 遇到意外令牌


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".

细节:


({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export

{ 默认为 a11yDark } 来自 './a11y-dark';


另外,我没有.babelrc文件。我试图添加.babelrc但它没有帮助(或者我做错了)


DIEA
浏览 349回答 2
2回答

慕的地6264312

如果您在尝试为组件导入 javascript 样式时遇到此错误,请确保您的目标是react-syntax-highlighter/dist/cjs/...而不是react-syntax-highlighter/dist/esm/...Jest 应该能够解析它。

白衣染霜花

所以,最后我解决了这个问题。我的最终配置是:babel.config.js :module.exports = {&nbsp; &nbsp; presets: ['@babel/preset-env', '@babel/preset-react'],};包.json :{&nbsp; "name": "project",&nbsp; "version": "1.0",&nbsp; "private": true,&nbsp; "dependencies": {&nbsp; &nbsp; "avro-js": "^1.9.1",&nbsp; &nbsp; "axios": "^0.19.0",&nbsp; &nbsp; "bootstrap": "^4.3.1",&nbsp; &nbsp; "jquery": "^3.4.1",&nbsp; &nbsp; "prop-types": "^15.7.2",&nbsp; &nbsp; "react": "^16.10.2",&nbsp; &nbsp; "react-dom": "^16.10.2",&nbsp; &nbsp; "react-redux": "^7.1.1",&nbsp; &nbsp; "react-scripts": "3.2.0",&nbsp; &nbsp; "react-syntax-highlighter": "^11.0.2",&nbsp; &nbsp; "reactstrap": "^8.0.1",&nbsp; &nbsp; "redux": "^4.0.4",&nbsp; &nbsp; "redux-thunk": "^2.3.0"&nbsp; },&nbsp; "jest": {&nbsp; &nbsp; "transformIgnorePatterns": [&nbsp; &nbsp; &nbsp; "/node_modules/(?!(react-syntax-highlighter)/)"&nbsp; &nbsp; ]&nbsp; },&nbsp; "devDependencies": {&nbsp; &nbsp; "@babel/cli": "^7.0.0",&nbsp; &nbsp; "@babel/core": "^7.0.0",&nbsp; &nbsp; "@babel/preset-env": "^7.0.0",&nbsp; &nbsp; "@babel/preset-react": "^7.0.0",&nbsp; &nbsp; "babel-core": "^7.0.0-bridge.0",&nbsp; &nbsp; "babel-eslint": "^10.0.3",&nbsp; &nbsp; "babel-jest": "^24.9.0",&nbsp; &nbsp; "babel-loader": "^8.0.0",&nbsp; &nbsp; "enzyme": "^3.10.0",&nbsp; &nbsp; "enzyme-adapter-react-16": "^1.15.1",&nbsp; &nbsp; "eslint": "^6.7.1",&nbsp; &nbsp; "jest": "^24.9.0",&nbsp; &nbsp; "jest-cli": "^24.9.0",&nbsp; &nbsp; "react-test-renderer": "^16.12.0",&nbsp; &nbsp; "redux-mock-store": "^1.5.3"&nbsp; },&nbsp; "scripts": {&nbsp; &nbsp; "start": "react-scripts start",&nbsp; &nbsp; "build": "react-scripts build",&nbsp; &nbsp; "test": "react-scripts test",&nbsp; &nbsp; "eject": "react-scripts eject"&nbsp; },&nbsp; "eslintConfig": {&nbsp; &nbsp; "extends": "react-app"&nbsp; },&nbsp; "browserslist": {&nbsp; &nbsp; "production": [&nbsp; &nbsp; &nbsp; ">0.2%",&nbsp; &nbsp; &nbsp; "not dead",&nbsp; &nbsp; &nbsp; "not op_mini all"&nbsp; &nbsp; ],&nbsp; &nbsp; "development": [&nbsp; &nbsp; &nbsp; "last 1 chrome version",&nbsp; &nbsp; &nbsp; "last 1 firefox version",&nbsp; &nbsp; &nbsp; "last 1 safari version"&nbsp; &nbsp; ]&nbsp; }}我不确定这是最好的解决方案,但至少它有效
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript