Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error boundary from masking a test error #11958

Merged
merged 1 commit into from Jan 3, 2018

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Jan 3, 2018

This code in ReactFiberScheduler can suppress "unexpected console warning" Errors thrown by our custom toWarnDev matcher, so I've changed the matcher to report those Errors to Jest as failed assertions instead. This means that we won't halt as quickly as before, but we'll still fail within the bounds of the expect().toWarnDev() callback so it's probably okay.

Before this change, the following test would unexpectedly pass:

class Parent extends React.Component {
  state = {};
  componentDidCatch(error) {
    this.setState({error}); // Render again
  }
  render() {
    return this.state.error ? null : <Child />;
  }
}

function Child() {
  const error = new Error('componentDidMount error');
  error.suppressReactErrorLogging = undefined;
  throw error;
}

ReactNoop.render(<Parent />);
expect(ReactNoop.flush).toWarnDev([]);

After this change, it fails unless the warning is explicitly expected or the entire block is wrapped in an expect(...).toThrow() block.

Note that the test would also have passed if we replaced expect(ReactNoop.flush).toWarnDev([]); above with ReactNoop.flush(); because of the way our default console "spy" queues Errors up to report in an afterEach block.

This PR also slightly improves the pretty printing message format in this case for when there are no remaining expected warnings or multiple ones. For example:

screen shot 2018-01-03 at 2 48 19 pm

screen shot 2018-01-03 at 2 48 31 pm

screen shot 2018-01-03 at 2 48 40 pm

Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me

@bvaughn bvaughn merged commit bb881f2 into facebook:master Jan 3, 2018
@bvaughn bvaughn deleted the toWarnDev-part-4 branch January 3, 2018 23:23
yenshih pushed a commit to yenshih/react that referenced this pull request Jan 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants