serialize.test.ts


Overview

This file contains unit tests for the unstable_serialize function from the swr library. The primary purpose of this test suite is to verify that unstable_serialize correctly converts various input arguments into their serialized string representations. Serialization is critical in swr (stale-while-revalidate) for caching and key generation to uniquely identify data fetching requests.

The tests ensure that:


Detailed Explanation

Imports

Test Suite: SWR - unstable_serialize

The describe block groups all tests related to the unstable_serialize function.

Test Case: should serialize arguments correctly

import { unstable_serialize } from 'swr'

const key1 = unstable_serialize(null)  // ''
const key2 = unstable_serialize('user')  // 'user'
const key3 = unstable_serialize([123, { id: 1 }, ['a', 'b']])
// key3 is a stable hash string representing the complex key

Important Implementation Details


Interaction with Other System Components


Diagram: Flowchart of Serialization Logic Tested

flowchart TD
    A[Input Argument] --> B{Is argument null or empty array?}
    B -- Yes --> C["Return '' (empty string)"]
    B -- No --> D{Is argument a string?}
    D -- Yes --> E["Return the string itself"]
    D -- No --> F["Use stableHash() to generate serialized string"]
    C --> G[Output]
    E --> G
    F --> G

Summary

This test suite is essential to maintain the integrity of SWR’s caching strategy by ensuring serialized keys are predictable and consistent.