我想在反应时运行单元测试,但是当我运行" npm test"时,它会显示我的错误:
navList.test.js: Unexpected token (12:26)
> 12 | let app = shallow(<NavList/>);
| ^
以下是我的代码:
navList.test.js
import React from 'react';
import {shallow, configure} from 'enzyme';
import {expect} from 'chai';
import {NavList} from '../components/common/nav/navList';
import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});
describe('NavList', function () {
it('NavList ', function () {
let app = shallow(<NavList/>);
expect(app.find('h2').length).to.equal(1);
});
});
的package.json :
"scripts": {
"test": "jest"
},
"devDependencies": {
"axios": "^0.16.2",
"babel-jest": "^22.1.0",
"babel-preset-react": "^6.23.0",
"bootstrap": "^4.0.0-beta",
"bootstrap-sass": "^3.3.7",
"chai": "^4.1.2",
"cross-env": "^5.0.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jest": "^22.1.4",
"jquery": "^3.1.1",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"mocha": "^5.0.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-test-renderer": "^16.2.0"
}
我在安装时遗漏了什么吗?
答案 0 :(得分:0)
看起来代码在jest尝试运行之前没有被编译(因此Unexpected token
错误)。
查看设置说明here。我最好的猜测是,您需要使用.babelrc
预设更新react
。