use-swr-error.test.tsx

Overview

This file contains a comprehensive suite of unit tests for the error handling behavior of the useSWR React hook, part of the SWR data fetching library. The tests verify that useSWR correctly manages error states, retries fetching on errors, triggers appropriate lifecycle callbacks (onError, onErrorRetry, onLoadingSlow, onSuccess), respects visibility and retry conditions, and handles edge cases such as component unmounting and synchronous/asynchronous errors.

The purpose of the file is to ensure robust, predictable error handling and retry logic within useSWR—a critical aspect for resilient data fetching in React applications.


Detailed Explanations

Test Suite: describe('useSWR - error', () => { ... })

The suite groups tests around how useSWR behaves when encountering errors during data fetching.


Individual Tests

Each test uses React Testing Library and utilities to render components using useSWR and simulate various error scenarios.


Common Testing Patterns


Key Tests and Their Purposes

1. should handle errors

Usage Example:

function Page() {
  const { data, error } = useSWR(key, () => createResponse(new Error('error!')))
  if (error) return <div>{error.message}</div>
  return <div>hello, {data}</div>
}

2. should trigger the onError event


3. should trigger error retry


4. should stop retrying when document is not visible


5. should not retry when shouldRetryOnError is disabled


6. should not retry when shouldRetryOnError function returns false


7. should retry when shouldRetryOnError function returns true


8. should trigger the onLoadingSlow and onSuccess event


9. should trigger limited error retries if errorRetryCount exists


10. should not trigger error retries if errorRetryCount is set to 0


11. should not clear error during revalidating until fetcher is finished successfully


12. should reset isValidating when an error occured synchronously


13. should reset isValidating when an error occured asynchronously


14. should dedupe onError events


15. Tests about avoiding revalidation when a key is active and has errors (isLoading and isValidating cases)


16. should trigger revalidation when first hook is unmount


Skipped Tests


Important Implementation Details & Algorithms


Interaction with Other Parts of the System


Visual Diagram: Component Interaction Flow in Tests

This flowchart represents the primary workflow and interactions modeled in these tests. Since this is a test file focused on hook behavior, the key components are the SWR hook, fetcher functions, and lifecycle callbacks.

flowchart TD
    A[Component with useSWR] --> B[useSWR Hook]
    B --> C[Fetcher Function]
    C -->|Returns Error| D[Error State]
    D --> E{Callbacks}
    E -->|onError| F[Error Callback Triggered]
    E -->|onErrorRetry| G[Retry Logic Executed]
    E -->|onLoadingSlow| H[Loading Slow Callback]
    E -->|onSuccess| I[Success Callback]

    G --> B
    B --> A
    style A fill:#f9f,stroke:#333,stroke-width:1px
    style B fill:#bbf,stroke:#333,stroke-width:1px
    style C fill:#fbf,stroke:#333,stroke-width:1px
    style D fill:#f99,stroke:#333,stroke-width:1px
    style E fill:#ffd,stroke:#333,stroke-width:1px
    style F fill:#fcf,stroke:#333,stroke-width:1px
    style G fill:#cfc,stroke:#333,stroke-width:1px
    style H fill:#cff,stroke:#333,stroke-width:1px
    style I fill:#cfc,stroke:#333,stroke-width:1px

Summary

This test file is essential for maintaining the integrity and resilience of the SWR hook’s error handling, ensuring that application components relying on it behave correctly under failure scenarios.