Test subscription cleanup and server snapshots
Test the store independently
A useful external store contract includes repeatable reads, immutable values, notification on changes and cleanup. Unit tests for that contract do not prove React scheduling. Each subscribe call owns its registration; its returned cleanup can run more than once without removing another registration.
import React,{useState,useEffect,useSyncExternalStore} from 'react';function createBoard(initial=0) { let snapshot=Object.freeze({available:initial}); const listeners=new Set(); return { getSnapshot:()=>snapshot, subscribe(callback){ const entry=()=>callback(); listeners.add(entry); return ()=>listeners.delete(entry); }, setAvailable(value){ if(!Number.isSafeInteger(value)||value<0) throw new RangeError('Invalid availability'); if(value===snapshot.available)return; snapshot=Object.freeze({available:value}); for(const listener of [...listeners])listener(); }, listenerCount:()=>listeners.size };}const board=createBoard(4);function Reader(){const data=useSyncExternalStore(board.subscribe,board.getSnapshot);return <p>Available: {data.available}</p>;}export default function App(){const [show,setShow]=useState(true);const [count,setCount]=useState(null);return <section><Reader/>{show&&<Reader/>}<button onClick={()=>setShow(v=>!v)}>Toggle second reader</button><button onClick={()=>setCount(board.listenerCount())}>Inspect subscriptions</button><p>Observed subscriptions: {count??'Not sampled'}</p></section>;}After the initial mount settles, inspect should report two active readers. Toggle the second reader off, then inspect again after cleanup to expect one. StrictMode may perform additional setup and cleanup; do not assert setup ran once. The sample button reads the current store count rather than using it as reactive UI state. The included Node fixture tests the subscription contract separately.
Supply a matching hydration snapshot
import React,{useState,useEffect,useSyncExternalStore} from 'react';function createBoard(initial=0) { let snapshot=Object.freeze({available:initial}); const listeners=new Set(); return { getSnapshot:()=>snapshot, subscribe(callback){ const entry=()=>callback(); listeners.add(entry); return ()=>listeners.delete(entry); }, setAvailable(value){ if(!Number.isSafeInteger(value)||value<0) throw new RangeError('Invalid availability'); if(value===snapshot.available)return; snapshot=Object.freeze({available:value}); for(const listener of [...listeners])listener(); }, listenerCount:()=>listeners.size };}export function createFixture(initial){const store=createBoard(initial);const initialSnapshot=store.getSnapshot();return {store,getServerSnapshot:()=>initialSnapshot};}const fixture=createFixture(5); // Same synthetic input on both sides; no real user data.export default function App(){const data=useSyncExternalStore(fixture.store.subscribe,fixture.store.getSnapshot,fixture.getServerSnapshot);return <section><p>Available kits: {data.available}</p><button onClick={()=>fixture.store.setAvailable(7)}>Refresh fixture</button></section>;}For this constant fixture the server and initial client hydration render should both show 5. Subsequent store updates should display 7. The optional third reader is used during server rendering and initial hydration, not as the ordinary live reader. A real request-specific store must be created per request, and the same initial data transferred to the client using the framework’s safe serialization facilities. Do not use this module singleton for request-specific or private data. An arbitrary fallback that differs between server and client can cause hydration mismatches. Missing the third argument prevents normal server rendering of the subscriber.
Scope store lifetime to a component tree
import React,{useState,useEffect,useSyncExternalStore} from 'react';function createBoard(initial=0) { let snapshot=Object.freeze({available:initial}); const listeners=new Set(); return { getSnapshot:()=>snapshot, subscribe(callback){ const entry=()=>callback(); listeners.add(entry); return ()=>listeners.delete(entry); }, setAvailable(value){ if(!Number.isSafeInteger(value)||value<0) throw new RangeError('Invalid availability'); if(value===snapshot.available)return; snapshot=Object.freeze({available:value}); for(const listener of [...listeners])listener(); }, listenerCount:()=>listeners.size };}function Desk({name,initial}){const [store]=useState(()=>createBoard(initial));const data=useSyncExternalStore(store.subscribe,store.getSnapshot);return <section><h2>{name}</h2><p>{data.available} kits</p><button onClick={()=>store.setAvailable(store.getSnapshot().available+1)}>Return kit</button></section>;}export default function App(){return <><Desk name="North desk" initial={2}/><Desk name="South desk" initial={5}/></>;}Returning a kit at North changes only North to 3; South stays 5. The initializer creates a local store without global side effects. Changes to initial after mounting do not reset this store; use explicit reset or a new identity if intended. This client example does not by itself implement SSR. Scope is an ownership decision, not an access-control boundary. Practice: test two independent stores, duplicate subscriptions and idempotent cleanup; then run the separate hydration checklist in an approved SSR harness.