-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow testing of errors thrown after render #828
Comments
First, I'll mention that Secondly, I don't think that this is possible. To be able to catch an error, we have to have our own Your alternative is solid and that's what I'd recommend you do. Wrap it in an error boundary. |
Thanks!
Is adding a try/catch inside RTL a possibility? I think it would be easier for me than wrapping in error boundary, and being able to test without the boundary component allows for better isolation. I've also found a few issues & SO questions that seem to be searching for the same thing I am. |
I'm not sure where we would put it for it to be effective 🤔
I'm not sure why. import {ErrorBoundary} from 'react-error-boundary'
test('error is thrown', async () => {
const fallbackRender = jest.fn(() => null)
render(
<ErrorBoundary fallbackRender={fallbackRender}>
<TodoApp />
</ErrorBoundary>
)
fireEvent.click(await screen.findByText('Add todo'));
await waitFor(() => expect(fallbackRender).toHaveBeenCalled())
expect(fallbackRender.mock.calls[0].error).toMatchInlineSnapshot(/* jest will update this */)
}) Pretty sure that will work. That's how I would test it if it were me. |
One other thing to consider is that it's unlikely this is desireable behavior. I'm not sure I understand the use case of expecting an error to occur in application code. I can understand it in library code though, and in this situation, this is how I'd go about it. |
I'm finding that react error boundaries do not actually "catch" errors in development (or they are at least rethrown), so I still get uncaught errors which cause my tests to fail. And for whatever reason, using |
@IanVS this might be a bit late but perhaps you are using create-react-app to scaffold your project. CRA shows an error overlay in development mode which you need to hide by pressing the escape key. it("Should display error boundary when there is a rendering error", () => {
render(
<ErrorBoundary>
<BuggyComponent flag={true} />
</ErrorBoundary>
);
// need to disable overlay in dev mode for create-react-app
fireEvent.keyDown(document.body, {
key: "Escape",
code: 27,
});
expect(screen.getByText(/mock test in error boundary/)).toBeInTheDocument();
}); something like this might work |
Cannot get it to work either, neither with |
@IanVS do you find a solution for that ? I'm having the same issue, i want to test for a element to thrown but it doesn't work, In every component that i want to test :c |
Nope, I don't have a solution, sorry. I just know some tests will fail in dev, and rely on my CI tests using a prod build. |
It's worth noting that an If you're wanting to catch an error that is thrown from an actual event, I'm not sure how you could - something along the testing chain seems to catch them first. |
I believe the root cause of this issue is that When testing errors thrown during rendering (and not involving const TestComponent = () => {
if (condition) {
throw new Error('Ooooops!');
}
return (<div>Test component</div>);
}; it('should throw an error', () => {
expect(() => {
TestComponent({ ...props }, { ...state });
}).toThrow('Ooooops!');
}); |
@kentcdodds The use case I've got here from is: I've got some page Foo, but this page should only be displayed if LaunchDarkly feature flag is enabled. If it's not enabled, we throw an error and let the error boundary catch it.
In this scenario the first render will be a null, and the second render should either be the thrown error, or the actual page render. But at this point I can't work how to 'wait till second render'. |
Render it inside an error boundary and assert on the fallback |
Will also be simpler in React 19 when we land #1297 |
Describe the feature you'd like:
I'd like to be able to write a passing test that verifies that when I
fireEvent
, an error is thrown. This test fails before it reaches theexpect
:I'd like something to the effect of:
Describe alternatives you've considered:
Adding an
<ErrorBoundary>
and testing both components together.The text was updated successfully, but these errors were encountered: