开玩笑“ <”上的意外令牌(15:29)

时间:2018-08-15 20:49:57

标签: javascript reactjs jestjs

我开玩笑地在我的React应用程序上工作,我正在尝试运行测试,但出现错误。当我使用"scripts": {"test": "react-scripts test --env=jsdom"}运行测试时,几乎所有测试都有效。我必须将测试仪更改为"test":"jest"才能使上一个测试正常工作,但是现在测试在编译时崩溃,因为多个shallow(<ReactPage />)中的<引发错误。

根据我对代码的理解,我的测试应该没问题。为解决这个错误,我必须在project.json文件中进行一些开玩笑的设置吗?

测试

import {shallow, mount, configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import ReactDOM from 'react-dom';
import jest from 'jest';
import ReactPage from './App';

configure({ adapter: new Adapter() });

it('renders ReactPage without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render( <ReactPage /> , div);
  ReactDOM.unmountComponentAtNode(div);
});

package.json

{
  "name": "client-charts",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "async": "^2.6.1",
    "moment": "^2.22.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.1",
    "react-scripts": "1.1.4",
    "recharts": "^1.1.0",
    "react-table": "^6.8.6",
    "react-snapshot": "^1.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && react-snapshot",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",
  "devDependencies": {
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "jest": "^20.0.4"
  }
}

1 个答案:

答案 0 :(得分:1)

您缺少处理JSX的步骤。如果您正在使用babel,请将react预设添加到.babelrc文件中。可以在这里找到更多信息:https://jestjs.io/docs/en/getting-started#additional-configuration