Registry / testing / msw-snapshot

msw-snapshot

JSON →
library5.3.0jsnpmunverified

Transparently create an API cache for testing using MSW. v5.3.0 (stable). Integrates with MSW v2 to snapshot network requests and replay them in tests, reducing reliance on live APIs. Key differentiators: supports snapshot update strategies (none/all/missing), customizable snapshot paths via createSnapshotPath, and callbacks for fetch events. Ships TypeScript types. Requires msw ^2.0.0 as a peer dependency.

npm install msw-snapshot
INSTALL
IMPORT
SIG · MSW-SNAPSHOT
M
msw-snapshot
testingjavascriptv5.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

snapshot
import { snapshot } from 'msw-snapshot'
import { snapshot } from 'snapshot'
Do not use bare 'snapshot' import – it's a different package.
context
import { context } from 'msw-snapshot'
Context provides namespace for snapshot isolation per test.
toHashString
import { toHashString } from 'msw-snapshot'
Utility for creating deterministic hash strings, useful in createSnapshotPath.

Sets up an MSW server with snapshot middleware, storing new snapshots on first run and replaying them on subsequent runs.

import { setupServer } from 'msw/node'; import { snapshot, context } from 'msw-snapshot'; const server = setupServer( snapshot({ basePath: './__snapshots__', updateSnapshots: 'missing', onFetchFromServer: (info) => { console.log('Fetched from server:', info.request.url); }, }) ); server.listen(); // Your test code here server.close();
Debug
Known issues
breakingMSW v2 compatibility only – msw-snapshot does not work with MSW v1.
fix
Upgrade to msw ^2.0.0 and use msw-snapshot v5.x.
affects: >=3.0.0 <4.0.0
deprecatedThe 'snapshot' import from 'snapshot' package is deprecated – use 'msw-snapshot'.
fix
Change import to 'import { snapshot } from 'msw-snapshot'.
affects: >=4.0.0
gotchaSnapshot identity relies on createSnapshotPath – naive usage may cause cache misses due to dynamic query params like timestamps.
fix
Use maskURLSearchParams to ignore volatile params, customizing createSnapshotPath accordingly.
affects: *
gotchaUsing updateSnapshots: 'all' can overwrite existing snapshots unintentionally, leading to test flakiness.
fix
Use 'missing' or 'none' for CI; reserve 'all' for local development when intentional.
affects: *
Errors
Common errors & fixes
TypeError: Cannot destructure property 'snapshot' of ... as it is undefined.
Incorrect import path: using 'import { snapshot } from 'snapshot'' instead of 'msw-snapshot'.
fix
Change to: import { snapshot } from 'msw-snapshot'.
Error: MSW v1 is not supported. Please upgrade to MSW v2.
Missing or outdated peer dependency msw (^2.0.0).
fix
Run: npm install msw@^2.0.0
Snapshot not matching – request URL contains a dynamic 't' parameter causing hash mismatch.
createSnapshotPath not configured to mask cache-busting query params.
fix
Use maskURLSearchParams in createSnapshotPath to ignore 't' or other dynamic params.
Upgrade
Version history
5.3.0latest on npm
Audit
Dependencies
mswrequiredRequired peer dependency for mocking service worker functionality.
Agent activity
6 hits · last 30 days
node
6
Resources
msw-snapshot — npm install msw-snapshot · libregistry