nock 13.3.8 → 13.5.0

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 CHANGED
@@ -1066,6 +1066,17 @@ if (!nock.isActive()) {
1066
1066
  }
1067
1067
  ```
1068
1068
 
1069
+ ### .clone()
1070
+
1071
+ You can clone a scope by calling `.clone()` on it:
1072
+
1073
+ ```js
1074
+ const scope = nock('http://example.test')
1075
+
1076
+ const getScope = scope.get('/').reply(200)
1077
+ const postScope = scope.clone().post('/').reply(200)
1078
+ ```
1079
+
1069
1080
  ## Restoring
1070
1081
 
1071
1082
  You can restore the HTTP interceptor to the normal unmocked behaviour by calling:
@@ -1497,6 +1508,45 @@ To set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BA
1497
1508
 
1498
1509
  - lockdown: use recorded nocks, disables all http calls even when not nocked, doesn't record
1499
1510
 
1511
+ ### Verifying recorded fixtures
1512
+
1513
+ Although you can certainly open the recorded JSON fixtures to manually verify requests recorded by nockBack - it's sometimes useful to put those expectations in your tests.
1514
+
1515
+ The `context.query` function can be used to return all of the interceptors that were recored in a given fixture.
1516
+
1517
+ By itself, this functions as a negative expectation - you can verify that certain calls do NOT happen in the fixture. Since `assertScopesFinished` can verify there are no _extra_ calls in a fixture - pairing the two methods allows you to verify the exact set of HTTP interactions recorded in the fixture. This is especially useful when re-recording for instance, a service that synchronizes via several HTTP calls to an external API.
1518
+
1519
+ **NB**: The list of fixtures is only available when reading. It will only be populated for nocks that are played back from fixtures.
1520
+
1521
+ #### Example
1522
+
1523
+ ```js
1524
+ it('#synchronize - synchronize with the external API', async localState => {
1525
+ const { nockDone, context } = await back('http-interaction.json')
1526
+
1527
+ const syncronizer = new Synchronizer(localState)
1528
+
1529
+ sycnronizer.syncronize()
1530
+
1531
+ nockDone()
1532
+
1533
+ context.assertScopesFinished()
1534
+
1535
+ expect(context.query()).toEqual(
1536
+ expect.arrayContaining([
1537
+ expect.objectContaining({
1538
+ method: 'POST',
1539
+ path: '/create/thing',
1540
+ }),
1541
+ expect.objectContaining({
1542
+ method: 'POST',
1543
+ path: 'create/thing',
1544
+ }),
1545
+ ]),
1546
+ )
1547
+ })
1548
+ ```
1549
+
1500
1550
  ## Common issues
1501
1551
 
1502
1552
  **"No match for response" when using got with error responses**
package/lib/back.js CHANGED
@@ -240,6 +240,23 @@ function load(fixture, options) {
240
240
  assertScopesFinished: function () {
241
241
  assertScopes(this.scopes, fixture)
242
242
  },
243
+ query: function () {
244
+ const nested = this.scopes.map(scope =>
245
+ scope.interceptors.map(interceptor => ({
246
+ method: interceptor.method,
247
+ uri: interceptor.uri,
248
+ basePath: interceptor.basePath,
249
+ path: interceptor.path,
250
+ queries: interceptor.queries,
251
+ counter: interceptor.counter,
252
+ body: interceptor.body,
253
+ statusCode: interceptor.statusCode,
254
+ optional: interceptor.optional,
255
+ })),
256
+ )
257
+
258
+ return [].concat.apply([], nested)
259
+ },
243
260
  }
244
261
 
245
262
  if (fixture && fixtureExists(fixture)) {
package/lib/scope.js CHANGED
@@ -286,6 +286,10 @@ class Scope extends EventEmitter {
286
286
  this.date = d || new Date()
287
287
  return this
288
288
  }
289
+
290
+ clone() {
291
+ return new Scope(this.basePath, this.scopeOptions)
292
+ }
289
293
  }
290
294
 
291
295
  function loadDefs(path) {
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "13.3.8",
10
+ "version": "13.5.0",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",
@@ -44,7 +44,7 @@
44
44
  "mocha": "^9.1.3",
45
45
  "npm-run-all": "^4.1.5",
46
46
  "nyc": "^15.0.0",
47
- "prettier": "3.0.3",
47
+ "prettier": "3.1.0",
48
48
  "proxyquire": "^2.1.0",
49
49
  "rimraf": "^3.0.0",
50
50
  "semantic-release": "^22.0.5",
package/types/index.d.ts CHANGED
@@ -267,10 +267,23 @@ declare namespace nock {
267
267
  }>
268
268
  }
269
269
 
270
+ interface InterceptorSurface {
271
+ method: string
272
+ uri: string
273
+ basePath: string
274
+ path: string
275
+ queries?: string
276
+ counter: number
277
+ body: string
278
+ statusCode: number
279
+ optional: boolean
280
+ }
281
+
270
282
  interface BackContext {
271
283
  isLoaded: boolean
272
284
  scopes: Scope[]
273
285
  assertScopesFinished(): void
286
+ query: InterceptorSurface[]
274
287
  }
275
288
 
276
289
  interface BackOptions {