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:
Empty inputs like
[]ornullserialize to an empty string.String keys serialize to themselves.
Complex nested arrays and objects serialize consistently using a stable hashing function (
stableHash).
Detailed Explanation
Imports
unstable_serialize: A function from theswrlibrary intended to create a serialized string representation of the input arguments, used primarily for caching keys.stableHash: An internal utility function fromswrthat produces a stable hash string for complex structured inputs (arrays, objects), ensuring consistent serialization for identical data.
Test Suite: SWR - unstable_serialize
The describe block groups all tests related to the unstable_serialize function.
Test Case: should serialize arguments correctly
Purpose: Validate the serialization output for different input types.
Tested Inputs and Expected Outputs:
[](empty array) →''(empty string)null→''(empty string)[1, { foo: 2, bar: 1 }, ['a', 'b', 'c']](complex nested structure) → output matches stableHash(...) of the input
Implementation Details:
The test uses Jest's expect and
toBeassertions to check equality.For complex objects, it compares the output of
unstable_serializewithstableHashdirectly, ensuring thatunstable_serializerelies onstableHashfor non-trivial inputs.
Usage Example:
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
The file runs tests in the
@edge-runtime/jest-environment, which suggests it is intended to be executed in an edge runtime environment (such as Vercel Edge Functions).unstable_serializeis considered unstable API (as indicated by the prefixunstable_), and its behavior might change in future releases.The usage of
stableHashinternally to generate serialized keys for complex structures guarantees that keys remain consistent between renders and data fetching, preventing unnecessary revalidation or cache misses.
Interaction with Other System Components
This test file directly tests functionality from the
swrlibrary, a popular React hook library for data fetching.It indirectly ensures that the caching mechanism in SWR works correctly by verifying key serialization.
The
stableHashfunction is internal toswrand ensures stable serialization; this test confirms that the externalunstable_serializefunction correctly delegates complex serialization tostableHash.This file does not directly interact with UI components or backend systems but validates core utility logic critical for caching and revalidation in data fetching workflows.
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
File Purpose: Unit tests for
unstable_serializeto ensure reliable serialization of cache keys.Functionality Tested: Handling of empty inputs, strings, and complex nested structures.
Key Dependencies:
stableHashfunction for stable serialization.System Role: Validates core utility for caching behavior in SWR's data fetching mechanism.
Environment: Edge runtime Jest environment.
This test suite is essential to maintain the integrity of SWR’s caching strategy by ensuring serialized keys are predictable and consistent.