Skip to content

Enzyme 3 couldn't test component when ComponentDidCatch happend #1255

@BlackGanglion

Description

@BlackGanglion

My Component:

class Error extends React.Component {
  render() {
    throw new Error('ErrorMessage');
    return <div />;
  }
}

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      hasError: false,
      error: null,
      errorInfo: null
    }
  }
  componentDidCatch(error, info) {
    this.setState({
      hasError: true,
      error,
      errorInfo: info
    });
  }
  render() {
    const { hasError } = this.state;
    if (this.state.hasError) {
      return <div className="error-view">error view</div>
    }
    return <Error />
  }
}

My test:

const app = mount(<App />);
const errorTip = app.setState({
  hasError: true,
});
expect(app.find('.error-view').length).to.equal(1);

When app is mount,app throws the error and I couldn't get the app to test the error view. How do I solve it?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions