initial-render.test.ts


Overview

initial-render.test.ts is an end-to-end (E2E) test file designed to validate the rendering behaviors of React applications that utilize the SWR data fetching library under complex asynchronous conditions. Specifically, it tests scenarios involving React Suspense integration, preload mechanisms, retry capabilities on fetch failures, mutation-triggered retries, and server component serialization of cache keys.

These tests are implemented using Playwright to simulate real user interactions in a browser context, navigating to example pages, triggering retry actions, and checking for expected UI states. The file ensures the robustness and correctness of SWR hooks when combined with React's Suspense, error boundaries, and mutation APIs across different React routing paradigms.


Detailed Explanation of Tests

Test Suite: rendering

The entire file is wrapped in a test.describe block labeled "rendering", grouping related rendering behavior tests.


1. Test: suspense with preload

test('suspense with preload', async ({ page }) => {
  await page.goto('./suspense-after-preload', { waitUntil: 'commit' })
  await page.getByRole('button', { name: 'preload' }).click()
  await expect(page.getByText('suspense-after-preload')).toBeVisible()
})

2. Test: should be able to retry in suspense with react 19 (app router)

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()
})

3. Test: should be able to retry in suspense with react 19 (pages router)

test('should be able to retry in suspense with react 19 (pages router)', async ({ page }) => {
  await page.goto('./suspense-retry-19', { 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()
})

4. Test: should be able to retry in suspense with mutate

test('should be able to retry in suspense with mutate', async ({ page }) => {
  await page.goto('./suspense-retry-mutate', { 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()
})

5. Test: should be able to use 'unstable_serialize' in server component

test('should be able to use `unstable_serialize` in server component', async ({ page }) => {
  await page.goto('./react-server-entry', { waitUntil: 'commit' })
  await expect(page.getByText('unstable_serialize: useSWR')).toBeVisible()
  await expect(
    page.getByText('infinite_unstable_serialize: $inf$useSWRInfinite')
  ).toBeVisible()
})

Important Implementation Details


Interaction with Other System Components


Usage Example

To run these tests, execute the Playwright test runner in the project root:

npx playwright test e2e/test/initial-render.test.ts

This will open headless browsers, run all defined tests, and report results indicating whether rendering and retry flows behave as expected.


Visual Diagram

The following flowchart illustrates the typical workflow covered by the tests in this file, focusing on data fetching, error handling, retry, and rendering flows within Suspense boundaries:

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]

Summary


End of Documentation for initial-render.test.ts