Mutation E2E Tests

Purpose

Mutation E2E Tests focus on validating the correctness and user experience of remote data mutation workflows within the broader SWR mutation feature. While the parent topic covers mutation hooks and UI feedback conceptually, these tests verify that the mutation lifecycle—from initiation through completion or failure—is reliably handled in a real browser environment. This ensures that optimistic updates, loading states, rollback on errors, and cache synchronization behave as expected under realistic usage.

Functionality

These end-to-end tests simulate user interactions that trigger remote mutations (such as POST or PUT requests) and observe the resulting UI states:

The test uses Playwright to automate browser actions and assertions, simulating real user behavior and verifying actual rendered output rather than internal hook states.

A critical snippet from the test illustrates this interaction:

await page.goto('./mutate-server-action')
await page.getByRole('button', { name: 'mutate' }).click()
await expect(page.getByText('isMutating: true')).toBeVisible()
await expect(page.getByText('data: ')).toBeVisible()
await page.waitForTimeout(500)
await expect(page.getByText('isMutating: false')).toBeVisible()
await expect(page.getByText('data: 10086')).toBeVisible()

This sequence validates that UI feedback aligns with mutation state transitions, ensuring a smooth user experience during asynchronous operations.

Integration

Mutation E2E Tests serve as a vital quality gate within the overall mutation subtopic by:

By bridging the gap between core mutation logic and user-facing behavior, these tests ensure mutation workflows remain robust and performant as part of the larger SWR ecosystem.

Mutation E2E Test Workflow Diagram

flowchart TD
    A[User Loads Mutation Page] --> B[User Clicks "mutate" Button]
    B --> C[Mutation Hook Starts Mutation]
    C --> D[UI Shows isMutating: true]
    D --> E[Server Processes Mutation Request]
    E --> F[Server Responds with New Data]
    F --> G[Cache & UI Update with New Data]
    G --> H[UI Shows isMutating: false]

This flowchart visualizes the end-to-end mutation lifecycle as tested by the E2E suite, from user interaction through server response to UI state synchronization.