Jest 和 Enzyme 使用 ES6 箭头函数测试 React 组件
Jest and Enzyme testing React Component with ES6 Arrow functions
我已将 Jest 和 Enzyme 添加到我的 React 项目中 运行ning webpack 2。
运行开个玩笑,在我的 React 组件中使用 ES6 箭头函数时,我遇到了 运行 错误。
组件代码示例如下:
import React, { Component } from 'react';
class Add extends Component {
constructor(props){
super(props);
this.state = this.defaultState();
}
defaultState = () => {
return {
name : ""
}
}
onChange = (e) =>{
this.setState({
name: e.target.value
});
}
handleAdd = (e) => {
e.preventDefault();
this.props.onAdd(this.state.name);
}
render= () => {
return (
<form>
<!-- more markup -->
</form>
);
}
}
export default Add;
Jest 在 运行 测试套件时失败,我是否需要在我的包 json / babelrc 中添加任何东西来启用箭头函数测试?
确保你安装了 babel-jest,然后在你的 bablerc 中你应该有 es2015,stage-0,并在预设中做出反应
我已将 Jest 和 Enzyme 添加到我的 React 项目中 运行ning webpack 2。 运行开个玩笑,在我的 React 组件中使用 ES6 箭头函数时,我遇到了 运行 错误。
组件代码示例如下:
import React, { Component } from 'react';
class Add extends Component {
constructor(props){
super(props);
this.state = this.defaultState();
}
defaultState = () => {
return {
name : ""
}
}
onChange = (e) =>{
this.setState({
name: e.target.value
});
}
handleAdd = (e) => {
e.preventDefault();
this.props.onAdd(this.state.name);
}
render= () => {
return (
<form>
<!-- more markup -->
</form>
);
}
}
export default Add;
Jest 在 运行 测试套件时失败,我是否需要在我的包 json / babelrc 中添加任何东西来启用箭头函数测试?
确保你安装了 babel-jest,然后在你的 bablerc 中你应该有 es2015,stage-0,并在预设中做出反应