我正在尝试在React组件上运行测试,但始终收到以下错误
/Users/emmy/Desktop/project/__tests__/components/ClientBasket/basket.test.jsx:1
import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at Runtime._execModule (node_modules/jest-runtime/build/index.js:1166:56)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.724 s
Ran all test suites matching /__tests__/i.
(node:79620) ExperimentalWarning: The fs.promises API is experimental
测试脚本:
import React from 'react';
import { shallow } from 'enzyme';
import Basket from '../../src/components/ClientBasket/basket';
const setUp = (props={}) => {
const component = shallow(<Basket {...props}/>);
return component;
};
describe('Basket', () => {
it('should render properly', () => {
const component = setUp();
console.log(component.debug())
const wrapper = component.find('.perproduct')
expect(wrapper.length).toBe(1);
})
})
如何调试此问题?我已经尝试了很多事情,但是找不到解决方法。
答案 0 :(得分:1)
首先,您需要进行babel-jest设置。 (https://jestjs.io/docs/en/getting-started)。
第二,确保您正在使用babel-preset-react(https://babeljs.io/docs/en/6.26.3/babel-preset-react)。您的babel配置应在“预设”部分中包含“ @ babel / preset-react”。
如果已配置所有配置,但仍无法使用,则很可能没有正确设置package.json。应该在笑话配置中包含这些行,以确保babel可以很好地与jsx文件一起播放。
"transform": {
"\\.js$": "<rootDir>/node_modules/babel-jest",
"\\.jsx$": "<rootDir>/node_modules/babel-jest"
},
"moduleFileExtensions": [
"js",
"jsx"
]
如果需要更多帮助,则应发布package.json和babel配置文件。