class name used for the snapshot filename
Optional snapshotDir: stringoptional directory override
Optional importMeta: ImportMetapass import.meta from calling module to resolve snapshot dir relative to it
In 'record' mode the store starts from a clean slate: an existing snapshot file is
NOT loaded (jsonlOnDisk/jsonlHeaderWritten stay false), so the first flush rewrites
the file in full via writeJsonlFull. Outside record mode the file is loaded as
before.
Private callOrdered per-key history of every recorded call event (file order), kept
alongside the last-wins data.calls so stateful call sites (iterator next()) can
be replayed in order.
Readonly className of the mocked class (from Base.name).
Private dataMaximum nested wrapping depth (default: Infinity). Set by @Mockable({ depth }).
Private dirtyPrivate jsonlPrivate jsonlPrivate pendingPrivate pendingPrivate pendingPrivate poolReadonly snapshotAbsolute path to the snapshot file on disk.
Private staticPrivate staticPrivate stringReverse index of pooled strings (value → ref) for O(1) lookups.
Private stringEnable symbol serialization (default: false). Set by @Mockable({ symbols: true }).
Private valuePrivate valuePrivate valueStatic Private _modeStatic modeCurrent global operating mode.
Private appendPrivate beginResets all in-memory snapshot state so the next flush rewrites the file in
full (lazy truncate). Invoked by setMode on the transition to 'record' so a
new record session starts from a clean slate — no cross-session _t:'c' duplicates.
Unlike release, this also resets poolCounter/valueCounter and
jsonlOnDisk/jsonlHeaderWritten (a released store still owns its on-disk file and is
lazily re-loaded).
Private depoolThe index-th (0-based) recorded occurrence of a call key, de-pooled.
Private getPrivate getPrivate isPrivate isDecides whether a serialized subtree should be pooled as a _t:'v' blob.
Counts the nested nodes cheaply first: subtrees within valuePoolCountLimit() nodes are
never pooled (no stringify cost). Larger subtrees are pooled only when the serialised
size exceeds valuePoolThreshold (UNIMOCK_VALUE_POOL_THRESHOLD, default 100 KB),
so node count guards the stringify cost while the byte threshold guards disk space.
Private jsonlPrivate loadPrivate loadSequence-aware replay lookup for unscoped (static) call keys: the k-th replay
call to callKey returns the k-th recorded occurrence (file order), generalising the
iterator next() sequence behaviour to statics. When the recorded sequence is exhausted,
falls back to the LAST recorded occurrence (warn-once) instead of missing. When a key was
recorded exactly once, this is identical to last-wins.
Returns undefined when the key has no recorded occurrences at all.
Private pendingPrivate poolPrivate poolPrivate poolPrivate pushPrivate readRecords a call entry (last-wins per callKey, plus ordered history for
sequence-aware replay). No-op outside record mode (defense in depth): the @Mockable()
wrappers never call this in replay/off, and the guard also protects direct API calls.
Frees this store's in-memory snapshot data so it can be garbage-collected
once no references to the store remain. Clears data.calls and the string/value pools
and pending indexes (all become GC-eligible). After release the store holds no snapshot
data; if it is requested again via getSnapshotStore a fresh store is created and
lazily re-loaded from disk.
Call this between host test files to bound memory in a long-running worker (e.g. vitest
isolate: false). Make sure the store has been flushed first.
Private resetResets all in-memory snapshot state. fileState additionally forgets the
on-disk file (next flush rewrites it in full) and counters resets the pooled
string/value counters. Instance config fields (className, snapshotPath, symbols,
depth) are left untouched.
Private resolvePrivate tryEncodes a uniform array of objects (>= 50 items, identical key list and
order) as a columnar SerializedCompactTable. Returns undefined when the
shape does not qualify, leaving the array untouched.
Private visitPrivate writePrivate writeSerialises records to the file descriptor, batching 500 lines per writeSync
(and per gzip member when compression is enabled) to bound memory and I/O syscalls.
Static Private assembleOptional error?: SerializedValueOptional refs: unknownBuilds a SnapshotCall object, omitting error when absent (replay is
tolerant, assembleCall callers pass undefined); refs is passed through as-is
(record() always supplies an explicit null-or-value so the v2 format always carries
the field, while get()/getAt() preserve undefined for legacy v1 entries).
Static setSets the global operating mode. Sweep: the transition into 'record' (from
replay/off) resets every cached store to a fresh record session, so the file is
rewritten in full on the first flush. Repeating setMode('record') while already in
record mode does NOT sweep: the current session stays intact.
Description
Per-class snapshot store that manages loading, recording, and persisting snapshot data.
Each decorated class gets its own
SnapshotStoreinstance, cached byclassName + snapshotDir. The store is shared across all instances of the decorated class within the same process.Features:
_t:'s'pooled-string lines,_t:'v'pooled-value lines, and_t:'c'call lines. Incremental append-only flush (no full rewrites after the first flush) keeps record O(N) and memory bounded.gunzipSyncwithout re-reading the whole file._t:'s'dictionary.UNIMOCK_VALUE_POOL*): large serialized subtrees (>100 KB default) are stored once as_t:'v'blobs and referenced from call entries. The dedup index survives flushes, so identical subtrees repeat across records are written only once (Sqquelizeinclude/build/settrees collapse ~4x).pooled_string/pooled_value.