http-snapshotter 0.4.2 → 0.4.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 +4 -0
- package/index.js +24 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,10 +25,14 @@ import { resolve, dirname } from "node:path";
|
|
|
25
25
|
import { start, startTestCase, endTestCase } from "http-snapshotter";
|
|
26
26
|
|
|
27
27
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
// if you are using an isolated test runner, then use a different directory per test (e.g. http-snapshots/test-case-1)
|
|
28
29
|
start({ snapshotDirectory: resolve(__dirname, "http-snapshots") });
|
|
29
30
|
|
|
30
31
|
test("Latest XKCD comic (ESM)", async (t) => {
|
|
32
|
+
// if you are *not* using an isolated test runner (e.g. tape), then `startTestCase` adds snapshots to separate directory
|
|
33
|
+
// Remove this line if it doesn't apply to your test runner
|
|
31
34
|
startTestCase('test-case-1');
|
|
35
|
+
|
|
32
36
|
const res = await fetch("https://xkcd.com/info.0.json");
|
|
33
37
|
const json = await res.json();
|
|
34
38
|
|
package/index.js
CHANGED
|
@@ -96,7 +96,7 @@ async function defaultSnapshotFileNameGenerator(request) {
|
|
|
96
96
|
filePrefix = [
|
|
97
97
|
'dynamodb',
|
|
98
98
|
matches[1], // e.g. eu-west-1
|
|
99
|
-
slugify(request.headers?.get?.('x-amz-target')?.split?.('.')?.pop?.() || ''),
|
|
99
|
+
slugify(request.headers?.get?.('x-amz-target')?.split?.('.')?.pop?.() || ''), // e.g. get-item, put-item
|
|
100
100
|
slugify(JSON.parse(await request.clone().text())?.TableName),
|
|
101
101
|
].filter(Boolean).join('-');
|
|
102
102
|
} else {
|
|
@@ -134,7 +134,7 @@ let snapshotSubDirectory = '';
|
|
|
134
134
|
/**
|
|
135
135
|
* @param {Request} request
|
|
136
136
|
*/
|
|
137
|
-
async function
|
|
137
|
+
async function getSnapshotFileInfo(request) {
|
|
138
138
|
const { fileSuffixKey, filePrefix } = await snapshotFileNameGenerator(request.clone());
|
|
139
139
|
|
|
140
140
|
// 15 characters are enough for uniqueness
|
|
@@ -153,6 +153,8 @@ async function getSnapshotFileName(request) {
|
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
+
/** @typedef {Awaited<ReturnType<getSnapshotFileInfo>>} SnapshotFileInfo */
|
|
157
|
+
|
|
156
158
|
// NOTE: This isn't going to work on a test runner that uses multiple processes / workers
|
|
157
159
|
/**
|
|
158
160
|
* @typedef {Promise<{
|
|
@@ -167,20 +169,12 @@ const readFiles = new Set();
|
|
|
167
169
|
const existingSubDirectories = new Set();
|
|
168
170
|
|
|
169
171
|
/**
|
|
170
|
-
* @param {
|
|
171
|
-
* @param {
|
|
172
|
-
* @param {
|
|
173
|
-
* @param {string} param.absoluteFilePath
|
|
174
|
-
* @param {string} param.fileName
|
|
175
|
-
* @param {string} param.fileSuffixKey
|
|
172
|
+
* @param {Request} request
|
|
173
|
+
* @param {Response} response
|
|
174
|
+
* @param {SnapshotFileInfo} snapshotFileInfo
|
|
176
175
|
*/
|
|
177
|
-
async function saveSnapshot({
|
|
178
|
-
|
|
179
|
-
response,
|
|
180
|
-
absoluteFilePath,
|
|
181
|
-
fileName,
|
|
182
|
-
fileSuffixKey,
|
|
183
|
-
}) {
|
|
176
|
+
async function saveSnapshot(request, response, snapshotFileInfo) {
|
|
177
|
+
const { absoluteFilePath, fileName, fileSuffixKey } = snapshotFileInfo;
|
|
184
178
|
// Prevent multiple tests from having same snapshot
|
|
185
179
|
if (alreadyWrittenFiles.has(absoluteFilePath)) {
|
|
186
180
|
return /** @type {ReadSnapshotReturnType} */ (alreadyWrittenFiles.get(absoluteFilePath));
|
|
@@ -264,9 +258,10 @@ const snapshotCache = {};
|
|
|
264
258
|
|
|
265
259
|
/**
|
|
266
260
|
* @param {Request} request
|
|
261
|
+
* @param {SnapshotFileInfo} snapshotFileInfo
|
|
267
262
|
*/
|
|
268
|
-
async function readSnapshot(request) {
|
|
269
|
-
const { absoluteFilePath, fileName, fileSuffixKey } =
|
|
263
|
+
async function readSnapshot(request, snapshotFileInfo) {
|
|
264
|
+
const { absoluteFilePath, fileName, fileSuffixKey } = snapshotFileInfo;
|
|
270
265
|
|
|
271
266
|
if (!snapshotCache[absoluteFilePath]) {
|
|
272
267
|
if (LOG_SNAPSHOT) {
|
|
@@ -340,9 +335,10 @@ async function sendResponse(request, snapshot) {
|
|
|
340
335
|
|
|
341
336
|
/**
|
|
342
337
|
* @param {Request} request
|
|
338
|
+
* @param {SnapshotFileInfo} snapshotFileInfo
|
|
343
339
|
*/
|
|
344
|
-
async function readSnapshotAndSendResponse(request) {
|
|
345
|
-
const { snapshot } = await readSnapshot(request);
|
|
340
|
+
async function readSnapshotAndSendResponse(request, snapshotFileInfo) {
|
|
341
|
+
const { snapshot } = await readSnapshot(request, snapshotFileInfo);
|
|
346
342
|
if (snapshot) {
|
|
347
343
|
return sendResponse(request, snapshot);
|
|
348
344
|
}
|
|
@@ -464,10 +460,14 @@ function start({
|
|
|
464
460
|
],
|
|
465
461
|
});
|
|
466
462
|
|
|
463
|
+
const cache = /** @type {WeakMap<Request, SnapshotFileInfo>} */ (new WeakMap());
|
|
464
|
+
|
|
467
465
|
// @ts-ignore
|
|
468
466
|
interceptor.on('request', async ({ request }) => {
|
|
469
467
|
if (['read', 'append'].includes(SNAPSHOT)) {
|
|
470
|
-
await
|
|
468
|
+
const snapshotFileInfo = await getSnapshotFileInfo(request);
|
|
469
|
+
cache.set(request, snapshotFileInfo);
|
|
470
|
+
await readSnapshotAndSendResponse(request, snapshotFileInfo);
|
|
471
471
|
}
|
|
472
472
|
});
|
|
473
473
|
interceptor.on(
|
|
@@ -475,7 +475,9 @@ function start({
|
|
|
475
475
|
'response',
|
|
476
476
|
/** @type {(params: { request: Request, response: Response }) => Promise<void>} */
|
|
477
477
|
async ({ request, response }) => {
|
|
478
|
-
const
|
|
478
|
+
const snapshotFileInfo = cache.get(request) || (await getSnapshotFileInfo(request));
|
|
479
|
+
cache.delete(request);
|
|
480
|
+
const { absoluteFilePath, fileName, fileSuffixKey } = snapshotFileInfo;
|
|
479
481
|
if (LOG_REQ) {
|
|
480
482
|
const summary = `----------\n${request.method} ${request.url}\nWould use file name: ${fileName}`;
|
|
481
483
|
if (LOG_REQ === '1' || LOG_REQ === 'summary') {
|
|
@@ -503,9 +505,7 @@ function start({
|
|
|
503
505
|
dirCreatePromise = fs.mkdir( /** @type {string} */(snapshotDirectory), { recursive: true });
|
|
504
506
|
}
|
|
505
507
|
await dirCreatePromise;
|
|
506
|
-
await saveSnapshot(
|
|
507
|
-
request, response, absoluteFilePath, fileName, fileSuffixKey,
|
|
508
|
-
});
|
|
508
|
+
await saveSnapshot(request, response, snapshotFileInfo);
|
|
509
509
|
}
|
|
510
510
|
},
|
|
511
511
|
);
|