Course contentsShow
← React
Lesson 2140 of 2,39647. Testing Patterns and Best PracticesFree lesson

Testing Error Information Passed to Fallback

Ensure the error boundary passes correct error details to fallback components for debugging and user feedback.

Testing Error Information Passed to Fallback

What You'll Learn

You'll learn how to verify that your error boundary correctly passes error details to fallback components, ensuring users see meaningful messages and developers get debugging information.

Why Error Details Matter

When your error boundary catches an error, it typically passes that error object (and possibly error info) to its fallback component as props. Your fallback UI might display different messages based on the error type, show a stack trace in development, or log error details for monitoring. Testing that these details actually arrive is crucial — otherwise, your beautifully designed fallback might be working with undefined or incomplete data.

Think of it like a postal system: you've tested that letters reach the post office (error is caught), and that they're delivered somewhere (fallback renders). Now you need to verify the contents of the envelope reach the recipient intact.

Testing the Error Payload

You've already learned to mock console.error, trigger errors with throwing components, and assert the fallback renders. Now, query specific elements in that fallback that should display error information:

it('passes error message to fallback component', () => {
  const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
  
  const testError = new Error('Database connection failed');
  const ThrowError = () => { throw testError; };
  
  render(
    <ErrorBoundary>
      <ThrowError />
    </ErrorBoundary>
  );
  
  // Verify the actual error message appears in the fallback
  expect(screen.getByText(/database connection failed/i)).toBeInTheDocument();
  
  consoleError.mockRestore();
});

You're testing that the error's message property made it through the boundary's componentDidCatch, into the fallback's props, and ultimately onto the screen where users can see it.

Key Takeaway: Testing error information delivery ensures your fallback components receive the diagnostic data they need to provide helpful user feedback and support debugging — verify that error messages, types, and context actually appear in your rendered fallback UI.