http-snapshotter 0.5.3-beta.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,6 +62,12 @@ Once you are done writing your tests, run your test runner on all your tests and
|
|
|
62
62
|
|
|
63
63
|
The tests of this library uses this library itself, check the `tests/` directory and try the tests `npm ci; npm test`.
|
|
64
64
|
|
|
65
|
+
## Limitations
|
|
66
|
+
|
|
67
|
+
1. MSW interceptor cannot mock `GET` calls with body. So trying to mock elasticsearch / opensearch `GET /<index>/_search` calls with body breaks. I ended up using `POST` only when unit testing, as `GET` is the right way to use with AWS `AmazonOpenSearchServiceReadOnlyAccess` IAM permission 🤷♂️.
|
|
68
|
+
|
|
69
|
+
2. Stripe SDK uses node http module in a way MSW interceptor cannot mock - [reference](https://github.com/mswjs/msw/issues/2259#issuecomment-2379379566). Solution mentioned in a comment there is to use Stripe SDK with `fetch` client - [reference](https://github.com/mswjs/msw/issues/2259#issuecomment-2422672039). Another workaround is to mock Stripe SDK's methods with your testing library of choice.
|
|
70
|
+
|
|
65
71
|
## About snapshot files and its names
|
|
66
72
|
|
|
67
73
|
A snapshot file name uniquely identifies a request. By default it is a combination of HTTP method + URL + body that makes a request unique (headers are ignored).
|
|
@@ -200,3 +206,24 @@ WARNING: This module isn't concurrent or thread safe. Make sure that:
|
|
|
200
206
|
1. within one worker only one test is being executed at a time. e.g. If you use `ava`, and you have multiple `test()` blocks in one file, you need to change it to run serially with `test.serial()`.
|
|
201
207
|
|
|
202
208
|
2. parallel tests don't update the same snapshot file at the same time (i.e. while you run with SNAPSHOT=update). Regardless, updating snapshots of multiple tests at the same time is not a great idea in my opinion, because reviewing the snapshots files are a pain, escpecially if you have a shared snapshot files.
|
|
209
|
+
|
|
210
|
+
## Ignoring requests from snapshots
|
|
211
|
+
|
|
212
|
+
Sometimes you may want certain requests to not be snapshotted (e.g., file uploads to S3 / object storage) so that you can layer function / result snapshotting on top. Use `attachSnapshotIgnoreRules()` to provide a function that determines which requests should be ignored:
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
import { attachSnapshotIgnoreRules, resetSnapshotIgnoreRules } from "http-snapshotter";
|
|
216
|
+
|
|
217
|
+
// Ignore analytics and health check requests
|
|
218
|
+
attachSnapshotIgnoreRules((request) => {
|
|
219
|
+
const url = new URL(request.url);
|
|
220
|
+
return url.hostname.includes('/upload');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// Reset to default (no requests ignored)
|
|
224
|
+
resetSnapshotIgnoreRules();
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**Behavior varies by mode:**
|
|
228
|
+
- `SNAPSHOT=update/append`: Ignored requests make real network calls but don't create snapshots
|
|
229
|
+
- `SNAPSHOT=read`: Ignored requests throw an error (tests shouldn't make real network calls)
|