我的酶测试直接来自某个教程:
import React from 'react';
import { mount } from 'enzyme';
import { expect } from 'chai';
describe('<Keypad />', () => {
it('should render children when passed in', () => {
const wrapper = mount(
<MyComponent>
<div className="unique" />
</MyComponent>
);
expect(wrapper.contains(<div className="unique" />)).to.equal(true);
});
});
我不断收到语法错误,意外令牌:
SyntaxError: test.js: Unexpected token (9:4)
7 | it('should render children when passed in', () => {
8 | const wrapper = mount(
> 9 | <MyComponent>
| ^
10 | <div className="unique" />
11 | </MyComponent>
12 | );
有什么想法吗?我安装了所有这些模块,并在package.json文件中配置:
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0",
"enzyme": "^2.3.0",
"mocha": "^2.5.3",
"react-addons-test-utils": "^15.1.0",
"redux-devtools": "^3.3.1",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
},
"dependencies": {
"compression": "^1.6.2",
"csurf": "^1.9.0",
"express-session": "^1.13.0",
"helmet": "^2.1.0",
"if-env": "^1.0.0",
"object.assign": "^4.0.3",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-redux": "^4.4.5",
"react-router": "^2.4.1",
"redux": "^3.5.2",
"request": "^2.72.0",
"winston": "^2.2.0"
}
我在Windows上运行npm test:
"test": "mocha src/home/test.js -c --compilers js:babel-register --recursive",
***** EDIT ******
这是我的组件文件:
// Library / Framework imports
import React, { Component } from 'react'
/*
* @class Home
* @description Display the home page React Component
*/
export default class Home extends Component {
render() {
return (
<section>
<h1>Home Page</h1>
<p>This is the home page</p>
</section>
)
}
}
这是我的测试文件:
// Library / Framework imports
import React from 'react'
import { mount, render, shallow } from 'enzyme'
import { expect } from 'chai'
import Home from './Home.jsx'
describe('<Home />', () => {
it('should render children when passed in', () => {
const wrapper = shallow(<Home />);
console.log(wrapper)
})
})
错误是:
SyntaxError: D:/home/test.js: Unexpected token (12:26)
10 |
11 | // console.log(SubmitButton)
> 12 | const wrapper = shallow(<Home />);
| ^
13 | console.log(wrapper)
14 |
15 | // expect(<SubmitButton />).contains()).to.equal(true)
我试过浅,渲染和装载。我在这里错过了什么?我使用的是最新的React 15版本。事实上所有最新的模块 - 只是安装了npm。我一定错过了对如何使用这个库的基本理解。请帮忙!
答案 0 :(得分:0)
<MyComponent>
只是一个示例,您需要在mount
<Keypad/>
const wrapper = mount(
<Keypad>
<div className="unique" />
</Keypad>
);
答案 1 :(得分:0)
您的组件文件名应类似于Home.js
不是 Home.jsx
,因此,当您要将其导入测试文件时,应为像这样:
import Home from './Home'
不是:
import Home from './Home.jsx'
另外,如果您解决了这个问题,那么您会遇到另一个问题,我将在this link中对此进行解释(这不是为酶设置适配器配置)
只需在您应用的根目录下运行以下代码:
npm i --save-dev enzyme-adapter-react-16
然后将这些行添加到测试中:
import Enzyme, {configure, shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
describe(
//what ever that was
);