本文介绍了反应组件上的Jest错误意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在运行npm test
时,我在React Component Name
上收到意外令牌.尝试阅读其他几个类似的问题,但似乎没有一个对我有用.我在下面添加了babelrc,package.json和我的测试文件的内容
I am getting Unexpected token on the React Component Name
while running npm test
. Tried reading several other similar questions but none seems to be working for me. I have added content of babelrc , package.json and my test file content below
<!-- content of .babelrc file -->
{ "presets": ["env"] }
<!-- content of package.son file -->
"dependencies": {
"react": "^16.2.0",
"react-bootstrap": "^0.32.1",
"react-dom": "^16.2.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.1",
"redux": "^3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-jest": "^23.0.0-alpha.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.3",
"jest": "^22.4.2",
"react-test-renderer": "^16.2.0"
},
"jest": {
"notify": true,
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"setupTestFrameworkScriptFile": "./src/setupTests.js",
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
}
<!-- Content of Test file App.test.js -->
import React from 'react';
import { shallow, mount } from 'enzyme';
import App from '../../src/components/App';
// describe what we are testing
describe('Render App Component', () => {
// make our assertion and what we expect to happen
it('should render without throwing an error', () => {
wrapper = shallow(<App />);
expect(wrapper.find('.app__wrapper').length).toEqual(1);
})
})
推荐答案
这是我解决问题的方法.
Here is how I resolved the issue.
- •将以下内容添加到您的.babelrc文件中,并确保.babelrc在根文件夹中
- • Add the following content to your .babelrc file and make sure .babelrc is in the root folder
{ "presets": ["env","react"] }
- •确保排除CSS,图像,SCSS,PDF,字体等静态资源.将以下内容添加到package.json中,如屏幕截图中突出显示
- • Make sure you exclude static assets like CSS, images, SCSS, PDF, fonts, etc. Add the following to package.json as highlighted in the screenshot
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
}
屏幕截图:
Screenshot:
这篇关于反应组件上的Jest错误意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!