@@ -91,7 +91,6 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
9191 - [ Requests made by ES Modules are not intercepted] ( #requests-made-by-es-modules-are-not-intercepted )
9292 - [ Axios] ( #axios )
9393 - [ Memory issues with Jest] ( #memory-issues-with-jest )
94- - [ Fake timers] ( #fake-timers )
9594- [ Debugging] ( #debugging )
9695- [ Contributing] ( #contributing )
9796- [ Contributors] ( #contributors )
@@ -1614,86 +1613,6 @@ One of the core principles of [Jest](https://jestjs.io/) is that it runs tests i
16141613It 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.
16151614[ Related issue with more details] ( https://github.com/nock/nock/issues/1817 ) .
16161615
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-
16971616## Debugging
16981617
16991618Nock uses node internals [ ` debuglog ` ] ( https://nodejs.org/api/util.html#utildebuglogsection-callbackg ) , so just run with environmental variable ` NODE_DEBUG ` set to ` nock:* ` .
0 commit comments