nock 14.0.7 → 14.0.9
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 +0 -81
- package/lib/common.js +18 -29
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -91,7 +91,6 @@ 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)
|
|
95
94
|
- [Debugging](#debugging)
|
|
96
95
|
- [Contributing](#contributing)
|
|
97
96
|
- [Contributors](#contributors)
|
|
@@ -1614,86 +1613,6 @@ One of the core principles of [Jest](https://jestjs.io/) is that it runs tests i
|
|
|
1614
1613
|
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.
|
|
1615
1614
|
[Related issue with more details](https://github.com/nock/nock/issues/1817).
|
|
1616
1615
|
|
|
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
|
-
|
|
1697
1616
|
## Debugging
|
|
1698
1617
|
|
|
1699
1618
|
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,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { common: debug } = require('./debug')
|
|
4
|
+
const timers = require('timers')
|
|
4
5
|
const url = require('url')
|
|
5
6
|
const util = require('util')
|
|
6
7
|
const http = require('http')
|
|
@@ -520,36 +521,24 @@ function deepEqual(expected, actual) {
|
|
|
520
521
|
const timeouts = new Set()
|
|
521
522
|
const immediates = new Set()
|
|
522
523
|
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
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)
|
|
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
|
+
}
|
|
546
534
|
}
|
|
535
|
+
const id = timer(cb, ...timerArgs)
|
|
536
|
+
ids.add(id)
|
|
537
|
+
return id
|
|
547
538
|
}
|
|
548
539
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
return id
|
|
552
|
-
}
|
|
540
|
+
const setTimeout = wrapTimer(timers.setTimeout, timeouts)
|
|
541
|
+
const setImmediate = wrapTimer(timers.setImmediate, immediates)
|
|
553
542
|
|
|
554
543
|
function clearTimer(clear, ids) {
|
|
555
544
|
ids.forEach(clear)
|
|
@@ -725,8 +714,8 @@ module.exports = {
|
|
|
725
714
|
percentDecode,
|
|
726
715
|
percentEncode,
|
|
727
716
|
removeAllTimers,
|
|
728
|
-
setImmediate
|
|
729
|
-
setTimeout
|
|
717
|
+
setImmediate,
|
|
718
|
+
setTimeout,
|
|
730
719
|
stringifyRequest,
|
|
731
720
|
convertFetchRequestToClientRequest,
|
|
732
721
|
}
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "14.0.
|
|
10
|
+
"version": "14.0.9",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"main": "./index.js",
|
|
23
23
|
"types": "types",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@mswjs/interceptors": "^0.39.
|
|
25
|
+
"@mswjs/interceptors": "^0.39.5",
|
|
26
26
|
"json-stringify-safe": "^5.0.1",
|
|
27
27
|
"propagate": "^2.0.0"
|
|
28
28
|
},
|