E2E Rendering Tests

Purpose

This subtopic focuses on end-to-end (E2E) testing of rendering behaviors in React applications using the SWR library, particularly under complex asynchronous conditions such as suspense and manual retry mechanisms. It addresses the need to verify that the UI correctly handles data fetching states, error boundaries, retries, and server-side data serialization within real browser environments. These tests ensure that SWR’s integration with React’s Suspense and error handling features works reliably across different routing strategies and mutation scenarios.

Functionality

The core functionality exercised in these E2E rendering tests includes:

These workflows simulate user interactions in a browser context, using Playwright to navigate pages, trigger retries, and assert visible UI states reflecting success or error.

Example Test Snippet

test('should be able to retry in suspense with react 19 (app router)', async ({ page }) => {
  await page.goto('./suspense-retry', { waitUntil: 'commit' })
  await expect(page.getByText('Something went wrong')).toBeVisible()
  await page.getByRole('button', { name: 'retry' }).click()
  await expect(page.getByText('data: SWR suspense retry works')).toBeVisible()
})

This test verifies that when the initial fetch fails, the error message is shown, and clicking the retry button triggers a successful re-fetch displaying the expected data.

Integration

E2E rendering tests serve as a critical validation layer that complements unit and integration tests within the main SWR project. They:

Together, these tests form a vital feedback loop for developers, preventing regressions in critical asynchronous rendering paths and increasing overall library stability.

Diagram

flowchart TD
  A[User Navigates to Page] --> B{Initial Data Fetch}
  B -->|Success| C[Render Data UI]
  B -->|Fail| D[Show Error Boundary]
  D --> E[Display Retry Button]
  E -->|User Clicks Retry| F[Trigger SWR mutate]
  F --> B
  C --> G[User Interacts with UI]
  G --> H[Optional Additional Fetches or Mutations]

This flowchart illustrates the core process tested by the E2E rendering tests: starting from a page load triggering data fetches, handling failure with error UI and retry options, and eventually rendering successful data states after retries. It highlights the interplay between user actions, SWR’s mutate function, and React’s Suspense/error handling.


By verifying these critical render and retry workflows in a full browser environment, this testing subtopic ensures that SWR’s complex asynchronous data handling integrates smoothly with React’s rendering paradigms, leading to robust, user-friendly applications.