nock 14.0.6 → 14.0.7

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 +81 -0
  2. package/lib/common.js +29 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -91,6 +91,7 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
91
91
  - [Requests made by ES Modules are not intercepted](#requests-made-by-es-modules-are-not-intercepted)
92
92
  - [Axios](#axios)
93
93
  - [Memory issues with Jest](#memory-issues-with-jest)
94
+ - [Fake timers](#fake-timers)
94
95
  - [Debugging](#debugging)
95
96
  - [Contributing](#contributing)
96
97
  - [Contributors](#contributors)
@@ -1613,6 +1614,86 @@ One of the core principles of [Jest](https://jestjs.io/) is that it runs tests i
1613
1614
  It does this by manipulating the modules cache of Node in a way that conflicts with how Nock monkey patches the builtin `http` and `https` modules.
1614
1615
  [Related issue with more details](https://github.com/nock/nock/issues/1817).
1615
1616
 
1617
+ ### Fake timers
1618
+
1619
+ ### Jest
1620
+
1621
+ To use `nock` in conjunction with `jest` fake timers, make sure you're using the "async" functions when advancing the
1622
+ timers, such as `jest.advanceTimersByTimeAsync()` or `jest.runAllTimersAsync()`. Otherwise, the timers will not be
1623
+ advanced correctly and you'll experience a timeout in your tests.
1624
+
1625
+ ```js
1626
+ test('should mock a request with fake timers', async () => {
1627
+ jest.useFakeTimers()
1628
+
1629
+ const scope = nock('https://example.com')
1630
+ .get('/path')
1631
+ .delay(1000)
1632
+ .reply(200, 'response')
1633
+
1634
+ // Simulate a request
1635
+ const request = got('https://example.com/path')
1636
+
1637
+ // Fast-forward time
1638
+ await jest.advanceTimersByTimeAsync(1000)
1639
+
1640
+ // Or advance all timers
1641
+ await jest.runAllTimersAsync()
1642
+
1643
+ // Wait for the request to complete
1644
+ const response = await request
1645
+
1646
+ expect(response.body).toBe('response')
1647
+ jest.useRealTimers() // Restore real timers after the test
1648
+ scope.done()
1649
+ })
1650
+ ```
1651
+
1652
+ In case you don't need testing delays, you can instruct `jest` to advance the timers automatically using the
1653
+ `advanceTimers` option
1654
+
1655
+ ```js
1656
+ jest.useFakeTimers({ advanceTimers: true })
1657
+ ```
1658
+
1659
+ ### Sinon
1660
+
1661
+ In a similar way to `jest`, if you are using `sinon` fake timers, you should use the `clock.tickAsync()` or
1662
+ `clock.runAllAsync()` methods to advance the timers correctly.
1663
+
1664
+ ```js
1665
+ it('should us sinon timers', async () => {
1666
+ clock = sinon.useFakeTimers()
1667
+ const scope = nock('https://example.com')
1668
+ .get('/path')
1669
+ .delay(1000)
1670
+ .reply(200, 'response')
1671
+
1672
+ // Simulate a request
1673
+ const request = got('https://example.com/path')
1674
+
1675
+ // Fast-forward time
1676
+ await clock.tickAsync(1000)
1677
+
1678
+ // Or run all timers
1679
+ await clock.runAllAsync()
1680
+
1681
+ // Wait for the request to complete
1682
+ const response = await request
1683
+
1684
+ expect(response.body).toBe('response')
1685
+ clock.restore()
1686
+ scope.done()
1687
+ })
1688
+ ```
1689
+
1690
+ Same applies for `sinon`, if you don't need testing delays, you can instruct `sinon` to advance the timers automatically
1691
+ using the `shouldAdvanceTime` option
1692
+
1693
+ ```js
1694
+ clock = sinon.useFakeTimers({ shouldAdvanceTime: true })
1695
+ ```
1696
+
1616
1697
  ## Debugging
1617
1698
 
1618
1699
  Nock uses node internals [`debuglog`](https://nodejs.org/api/util.html#utildebuglogsection-callbackg), so just run with environmental variable `NODE_DEBUG` set to `nock:*`.
package/lib/common.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { common: debug } = require('./debug')
4
- const timers = require('timers')
5
4
  const url = require('url')
6
5
  const util = require('util')
7
6
  const http = require('http')
@@ -521,24 +520,36 @@ function deepEqual(expected, actual) {
521
520
  const timeouts = new Set()
522
521
  const immediates = new Set()
523
522
 
524
- const wrapTimer =
525
- (timer, ids) =>
526
- (callback, ...timerArgs) => {
527
- const cb = (...callbackArgs) => {
528
- try {
529
- // eslint-disable-next-line n/no-callback-literal
530
- callback(...callbackArgs)
531
- } finally {
532
- ids.delete(id)
533
- }
523
+ const _setImmediate = (callback, ...timerArgs) => {
524
+ const cb = (...callbackArgs) => {
525
+ try {
526
+ // eslint-disable-next-line n/no-callback-literal
527
+ callback(...callbackArgs)
528
+ } finally {
529
+ immediates.delete(id)
530
+ }
531
+ }
532
+
533
+ const id = setImmediate(cb, 0, ...timerArgs)
534
+
535
+ immediates.add(id)
536
+ return id
537
+ }
538
+
539
+ const _setTimeout = (callback, ...timerArgs) => {
540
+ const cb = (...callbackArgs) => {
541
+ try {
542
+ // eslint-disable-next-line n/no-callback-literal
543
+ callback(...callbackArgs)
544
+ } finally {
545
+ timeouts.delete(id)
534
546
  }
535
- const id = timer(cb, ...timerArgs)
536
- ids.add(id)
537
- return id
538
547
  }
539
548
 
540
- const setTimeout = wrapTimer(timers.setTimeout, timeouts)
541
- const setImmediate = wrapTimer(timers.setImmediate, immediates)
549
+ const id = setTimeout(cb, ...timerArgs)
550
+ timeouts.add(id)
551
+ return id
552
+ }
542
553
 
543
554
  function clearTimer(clear, ids) {
544
555
  ids.forEach(clear)
@@ -714,8 +725,8 @@ module.exports = {
714
725
  percentDecode,
715
726
  percentEncode,
716
727
  removeAllTimers,
717
- setImmediate,
718
- setTimeout,
728
+ setImmediate: _setImmediate,
729
+ setTimeout: _setTimeout,
719
730
  stringifyRequest,
720
731
  convertFetchRequestToClientRequest,
721
732
  }
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "14.0.6",
10
+ "version": "14.0.7",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",