Skip to content

Commit f7bbc37

Browse files
committedAug 6, 2020
Add an example of nock integration with retrying
Fixes #1392
1 parent 7072198 commit f7bbc37

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎readme.md

+26
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,32 @@ nock('https://sindresorhus.com')
19241924
})();
19251925
```
19261926

1927+
Bear in mind, that by default `nock` mocks only one request. Got will [retry](#retry) on failed requests by default, causing a `No match for request ...` error. The solution is to either disable retrying (set `options.retry` to `0`) or call `.persist()` on the mocked request.
1928+
1929+
```js
1930+
const got = require('got');
1931+
const nock = require('nock');
1932+
1933+
const scope = nock('https://sindresorhus.com')
1934+
.get('/')
1935+
.reply(500, 'Internal server error')
1936+
.persist();
1937+
1938+
(async () => {
1939+
try {
1940+
await got('https://sindresorhus.com')
1941+
} catch (error) {
1942+
console.log(error.response.body);
1943+
//=> 'Internal server error'
1944+
1945+
console.log(error.response.retryCount);
1946+
//=> 2
1947+
}
1948+
1949+
scope.persist(false);
1950+
})();
1951+
```
1952+
19271953
For real integration testing we recommend using [`ava`](https://github.com/avajs/ava) with [`create-test-server`](https://github.com/lukechilds/create-test-server). We're using a macro so we don't have to `server.listen()` and `server.close()` every test. Take a look at one of our tests:
19281954

19291955
```js

0 commit comments

Comments
 (0)
Please sign in to comment.