http-snapshotter 0.2.3 → 0.2.4
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 +2 -0
- package/index.js +34 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,6 +53,8 @@ There is also a `SNAPSHOT=ignore` option to neither read nor write from snapshot
|
|
|
53
53
|
|
|
54
54
|
Tip: When you do `SNAPSHOT=update` to create snapshots, run it against a single test, so you know what exact snapshots that one test created/updated.
|
|
55
55
|
|
|
56
|
+
Log read/saved snapshots by setting LOG_SNAPSHOT=1 env variable. Log requests with LOG_REQ=1 or LOG_REQ=summary (to just print request HTTP method, url and snapshot file that it would use).
|
|
57
|
+
|
|
56
58
|
Once you are done writing your tests, run your test runner on all your tests and then take a look at `<snapshots directory>/unused-snapshots.log` file to see which snapshot files haven't been used by your final test suite. You can delete unused snapshot files.
|
|
57
59
|
|
|
58
60
|
The tests of this library uses this library itself, check the `tests/` directory and try the tests `npm ci; npm test`.
|
package/index.js
CHANGED
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
* Here onwards run test runner without SNAPSHOT env variable or SNAPSHOT=read
|
|
14
14
|
* You can use SNAPSHOT=ignore to neither read not write snapshots, for testing on real
|
|
15
15
|
* network operations.
|
|
16
|
+
*
|
|
17
|
+
* Log read/saved snapshots by setting LOG_SNAPSHOT=1 env variable.
|
|
16
18
|
*
|
|
17
19
|
* Unused snapshot files will be written into a log file named 'unused-snapshots.log'.
|
|
18
20
|
* You can delete those files manually.
|
|
19
|
-
*
|
|
20
|
-
* Log requests with LOG_REQ=1 env variable
|
|
21
|
+
*
|
|
22
|
+
* Log requests with LOG_REQ=1 or LOG_REQ=summary (to just print summary) env variable
|
|
21
23
|
* or node.js built-in NODE_DEBUG=http,http2
|
|
22
24
|
*
|
|
23
25
|
* More docs at the end of this file, find the exported methods.
|
|
@@ -33,7 +35,7 @@ const { resolve } = require('node:path');
|
|
|
33
35
|
|
|
34
36
|
// Environment variable SNAPSHOT = update / ignore / read (default)
|
|
35
37
|
const SNAPSHOT = process.env.SNAPSHOT || 'read';
|
|
36
|
-
const
|
|
38
|
+
const { LOG_REQ, LOG_SNAPSHOT } = process.env;
|
|
37
39
|
const unusedSnapshotsLogFile = 'unused-snapshots.log';
|
|
38
40
|
/**
|
|
39
41
|
* @type {import("node:fs").PathLike | null}
|
|
@@ -159,13 +161,16 @@ const readFiles = new Set();
|
|
|
159
161
|
*/
|
|
160
162
|
async function saveSnapshot(request, response) {
|
|
161
163
|
const { absoluteFilePath, fileName, fileSuffixKey } = await getSnapshotFileName(request);
|
|
162
|
-
// console.log(fileName);
|
|
163
164
|
|
|
164
165
|
// Prevent multiple tests from having same snapshot
|
|
165
166
|
if (alreadyWrittenFiles.has(absoluteFilePath)) {
|
|
166
167
|
return /** @type {ReadSnapshotReturnType} */ (alreadyWrittenFiles.get(absoluteFilePath));
|
|
167
168
|
}
|
|
168
169
|
|
|
170
|
+
if (LOG_SNAPSHOT) {
|
|
171
|
+
console.debug('Writing:', fileName);
|
|
172
|
+
}
|
|
173
|
+
|
|
169
174
|
/** @returns {ReadSnapshotReturnType} */
|
|
170
175
|
const saveFreshSnapshot = async () => {
|
|
171
176
|
let requestBody;
|
|
@@ -238,9 +243,11 @@ const snapshotCache = {};
|
|
|
238
243
|
*/
|
|
239
244
|
async function readSnapshot(request) {
|
|
240
245
|
const { absoluteFilePath, fileName, fileSuffixKey } = await getSnapshotFileName(request);
|
|
241
|
-
// console.log(fileName);
|
|
242
246
|
|
|
243
247
|
if (!snapshotCache[absoluteFilePath]) {
|
|
248
|
+
if (LOG_SNAPSHOT) {
|
|
249
|
+
console.debug('Reading:', fileName);
|
|
250
|
+
}
|
|
244
251
|
let json;
|
|
245
252
|
try {
|
|
246
253
|
json = await fs.readFile(absoluteFilePath, 'utf-8');
|
|
@@ -256,8 +263,8 @@ async function readSnapshot(request) {
|
|
|
256
263
|
headers: Object.fromEntries([...request.headers.entries()]),
|
|
257
264
|
body: reqBody,
|
|
258
265
|
},
|
|
259
|
-
|
|
260
|
-
|
|
266
|
+
fileName,
|
|
267
|
+
fileSuffixKey,
|
|
261
268
|
});
|
|
262
269
|
throw new Error('Network request not mocked');
|
|
263
270
|
} else {
|
|
@@ -453,16 +460,26 @@ function start({
|
|
|
453
460
|
async ({ request, response }) => {
|
|
454
461
|
if (LOG_REQ) {
|
|
455
462
|
const { fileName, fileSuffixKey } = await getSnapshotFileName(request);
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
463
|
+
const summary = `----------\n${request.method} ${request.url}\nWould use file name: ${fileName}`;
|
|
464
|
+
if (LOG_REQ === 'summary') {
|
|
465
|
+
console.debug(summary);
|
|
466
|
+
} else {
|
|
467
|
+
console.debug(`${summary}\n----------\n`, {
|
|
468
|
+
request: {
|
|
469
|
+
url: request.url,
|
|
470
|
+
method: request.method,
|
|
471
|
+
headers: Object.fromEntries([...request.headers.entries()]),
|
|
472
|
+
body: await request.clone().text(),
|
|
473
|
+
},
|
|
474
|
+
response: {
|
|
475
|
+
status: response.status,
|
|
476
|
+
statusText: response.statusText,
|
|
477
|
+
headers: Object.fromEntries([...response.headers.entries()]),
|
|
478
|
+
body: await response.clone().text(),
|
|
479
|
+
},
|
|
480
|
+
wouldUseFileSuffixKey: fileSuffixKey,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
466
483
|
}
|
|
467
484
|
if (SNAPSHOT === 'update') {
|
|
468
485
|
if (!dirCreatePromise) {
|