http-snapshotter 0.3.0 → 0.3.1

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.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/index.js +12 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -49,9 +49,9 @@ You will see a file named `get-xkcd-com-info-0-arAlFb5gfcr9aCN.json` created in
49
49
  Then onwards running: `node test.js` or `SNAPSHOT=read node test.js` will ensure HTTP network calls are all read from a snapshot file.
50
50
  In this mode, http-snapshotter will prevent any real HTTP calls from happening by failing the test (if it didn't have a snapshot file) and print out the request details and the snapshot file name it should have had.
51
51
 
52
- There is also a `SNAPSHOT=ignore` option to neither read nor write from snapshot files and do real network requests instead. This could be useful while writing a new test.
52
+ For adding new snapshots without touching existing snapshots use `SNAPSHOT=append`. There is also a `SNAPSHOT=ignore` option to neither read nor write from snapshot files and do real network requests instead. These could be useful while writing a new test.
53
53
 
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.
54
+ Tip: When you do `SNAPSHOT=update` or `SNAPHOT=append` to create snapshots, run it against a single test, so you know what exact snapshots that one test created/updated.
55
55
 
56
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
57
 
package/index.js CHANGED
@@ -10,7 +10,9 @@
10
10
  * SNAPSHOT=update <test runner command>
11
11
  * e.g. SNAPSHOT=update tape tests/**\/*.js | tap-diff
12
12
  *
13
- * Here onwards run test runner without SNAPSHOT env variable or SNAPSHOT=read
13
+ * Here onwards run test runner without SNAPSHOT env variable or SNAPSHOT=read.
14
+ * For adding new snapshots without touching existing snapshots use SNAPSHOT=append.
15
+ *
14
16
  * You can use SNAPSHOT=ignore to neither read not write snapshots, for testing on real
15
17
  * network operations.
16
18
  *
@@ -33,7 +35,7 @@ const { createHash } = require('node:crypto');
33
35
  const { promises: fs } = require('node:fs');
34
36
  const { resolve } = require('node:path');
35
37
 
36
- // Environment variable SNAPSHOT = update / ignore / read (default)
38
+ // Environment variable SNAPSHOT = update / append / ignore / read (default)
37
39
  const SNAPSHOT = process.env.SNAPSHOT || 'read';
38
40
  const { LOG_REQ, LOG_SNAPSHOT } = process.env;
39
41
  const unusedSnapshotsLogFile = 'unused-snapshots.log';
@@ -249,6 +251,7 @@ async function readSnapshot(request) {
249
251
  // Fail any test that fires a real network request (without snapshot)
250
252
  // @ts-ignore
251
253
  if (err.code === 'ENOENT') {
254
+ if (SNAPSHOT === 'append') return {};
252
255
  const reqBody = await request.clone().text();
253
256
  console.error('No network snapshot found for request with cache keys:', {
254
257
  request: {
@@ -312,7 +315,10 @@ async function sendResponse(request, snapshot) {
312
315
  */
313
316
  async function readSnapshotAndSendResponse(request) {
314
317
  const { snapshot } = await readSnapshot(request);
315
- return sendResponse(request, snapshot);
318
+ if (snapshot) {
319
+ return sendResponse(request, snapshot);
320
+ }
321
+ return undefined;
316
322
  }
317
323
 
318
324
  /**
@@ -421,7 +427,7 @@ function start({
421
427
 
422
428
  // @ts-ignore
423
429
  interceptor.on('request', async ({ request }) => {
424
- if (SNAPSHOT === 'read') {
430
+ if (['read', 'append'].includes(SNAPSHOT)) {
425
431
  await readSnapshotAndSendResponse(request);
426
432
  }
427
433
  });
@@ -430,8 +436,8 @@ function start({
430
436
  'response',
431
437
  /** @type {(params: { request: Request, response: Response }) => Promise<void>} */
432
438
  async ({ request, response }) => {
439
+ const { fileName, fileSuffixKey } = await getSnapshotFileName(request);
433
440
  if (LOG_REQ) {
434
- const { fileName, fileSuffixKey } = await getSnapshotFileName(request);
435
441
  const summary = `----------\n${request.method} ${request.url}\nWould use file name: ${fileName}`;
436
442
  if (LOG_REQ === 'summary') {
437
443
  console.debug(summary);
@@ -453,7 +459,7 @@ function start({
453
459
  });
454
460
  }
455
461
  }
456
- if (SNAPSHOT === 'update') {
462
+ if (SNAPSHOT === 'update' || (SNAPSHOT === 'append' && !readFiles.has(fileName))) {
457
463
  if (!dirCreatePromise) {
458
464
  dirCreatePromise = fs.mkdir( /** @type {string} */(snapshotDirectory), { recursive: true });
459
465
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-snapshotter",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Snapshot HTTP requests for tests (node.js)",
5
5
  "main": "index.cjs",
6
6
  "types": "index.d.ts",