javascript - 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
但没有帮助(或者我做错了)
解决方案
如果您在尝试导入组件的 javascript 样式时遇到此错误,请确保您的目标是
react-syntax-highlighter/dist/cjs/...
而不是
react-syntax-highlighter/dist/esm/...
Jest 应该能够解析它。
推荐阅读
- html - 从 Web 下载 CHM 文件时出现问题
- r - ggplot,在套索中找不到对象“值”
- php - 路由无法从表单中获取 id(缺少 [Route: update_user] 所需的参数)
- angular - 在 Angular 中显示来自 util 服务的弹出窗口
- java - OnItemClickListener 不适用于 AutoCompleteTextView
- spring - 在客户端重新启动之前,使用 docker-compose 无法访问 Spring Config 服务器
- mysql - 使用带有 Sequelize ORM 和文字的原始 SQL 查询
- sql - 如果连接表不返回结果,则不返回任何行
- sql-server - 按给定等级查找所有实体
- scala - 如何从嵌套的 JSON 中提取密钥