【问题标题】:Jest and Enzyme testing React Component with ES6 Arrow functionsJest 和 Enzyme 使用 ES6 箭头函数测试 React 组件
【发布时间】:2017-11-30 18:26:18
【问题描述】:

我已将 Jest 和 Enzyme 添加到运行 webpack 2 的 React 项目中。 在运行 jest 时,我在我的 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 中添加任何东西来启用箭头功能测试吗?

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    确保你已经安装了 babel-jest,然后在你的 bablerc 中你应该有 es2015,stage-0,并在预设中做出反应

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 2018-08-20
      • 2017-04-13
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      相关资源
      最近更新 更多