You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(snapshot-agent): add normalizeBody and normalizeQuery options
Adds two new options to SnapshotAgent and SnapshotRecorder for partial
request matching:
- `normalizeBody(body)` — normalizes the request body before hashing,
e.g. to strip volatile fields like timestamps from JSON payloads
- `normalizeQuery(params)` — normalizes query parameters (as
URLSearchParams) before hashing, e.g. to strip cache-busting params
Both options pair with the existing `matchBody`/`matchQuery` boolean
toggles and run at both record and playback time so hashes stay
consistent across both sides.
Copy file name to clipboardExpand all lines: docs/docs/api/SnapshotAgent.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,9 @@ new SnapshotAgent([options])
27
27
-**ignoreHeaders**`Array<String>` - Headers to ignore during request matching
28
28
-**excludeHeaders**`Array<String>` - Headers to exclude from snapshots (for security)
29
29
-**matchBody**`Boolean` - Whether to include request body in matching. Default: `true`
30
+
-**normalizeBody**`Function` - Optional function `(body) => string` to normalize the request body before matching (e.g. strip volatile fields like timestamps). Only used when `matchBody` is `true`.
30
31
-**matchQuery**`Boolean` - Whether to include query parameters in matching. Default: `true`
32
+
-**normalizeQuery**`Function` - Optional function `(query: URLSearchParams) => string` to normalize query parameters before matching (e.g. strip volatile params like cache-busters). Only used when `matchQuery` is `true`.
31
33
-**caseSensitive**`Boolean` - Whether header matching is case-sensitive. Default: `false`
32
34
-**shouldRecord**`Function` - Callback to determine if a request should be recorded
33
35
-**shouldPlayback**`Function` - Callback to determine if a request should be played back
By default (`matchBody: true`) the full request body string is included in the snapshot key. Set it to `false` to ignore the body entirely, or use `normalizeBody` to strip volatile fields (like timestamps) before matching:
116
+
117
+
```javascript
118
+
constagent=newSnapshotAgent({
119
+
mode:'playback',
120
+
snapshotPath:'./snapshots.json',
121
+
122
+
// Match on everything except the timestamp field
123
+
normalizeBody: (body) => {
124
+
if (!body) return''
125
+
constparsed=JSON.parse(String(body))
126
+
deleteparsed.timestamp
127
+
returnJSON.stringify(parsed)
128
+
}
129
+
})
130
+
```
131
+
132
+
`normalizeBody` receives the raw body (`string | Buffer | null | undefined`) and must return a `string`. It runs at both record and playback time so the hash is consistent. Two requests match the same snapshot whenever their normalized strings are identical.
133
+
111
134
### Header Filtering
112
135
113
136
Control which headers are used for request matching and what gets stored in snapshots:
0 commit comments