In Jest, `nock.restore()` prevents subsequent mocks from working
See original GitHub issueWhat is the expected behavior? Individual tests pass, running them as part of a describe block should also pass
What is the actual behavior? The first time a test makes a request its fine, but all of the subsequent requests are triggering errors:
Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14)
How to reproduce the issue Running any of these tests individually works great, running the describe block the first one works, the 2nd and 3rd throw the error
import axios from 'axios'
import httpAdapter from 'axios/lib/adapters/http'
import nock from 'nock'
const host = 'http://localhost';
axios.defaults.baseURL = host;
axios.defaults.adapter = httpAdapter;
afterEach(() => {
nock.restore();
});
describe('hmm', () => {
test('1', async () => {
nock(host).get('/test1').reply(200, 'hello world');
const rsp = await axios.get('/test1');
expect(rsp.data).toBe('hello world');
});
test('2', async () => {
nock(host).get('/test2').reply(200, 'hello world');
const rsp = await axios.get('/test2');
expect(rsp.data).toBe('hello world');
});
test('3', async () => {
nock(host).get('/test3').reply(200, 'hello world');
const rsp = await axios.get('/test3');
expect(rsp.data).toBe('hello world');
});
});
Versions
Software | Version(s) |
---|---|
Nock | 13.0.2 |
Node | 12.4.0 |
Jest/core | 26.1.0 |
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:13 (4 by maintainers)
Top Results From Across the Web
node.js - Nock not working for multiple tests running together
Solution 1: Remove nock.restore() . Solution 2: Have this before() method in your test.
Read more >API mock testing with Nock - CircleCI
Clearing mocks and blocks keeps them from interfering with subsequent tests, and allows the other tests to make HTTP requests. To clear mocks ......
Read more >nock reset between tests | The AI Search Engine You Control
In Jest, `nock.restore()` prevents subsequent mocks from working ... Github.com > nock > nock. Calling nock.activate() before the first ...
Read more >How to use the nock.restore function in nock - Snyk
public done() { // scope.done() will throw an error if there are expected api calls that have not happened. // So ensures that...
Read more >Nock: HTTP Mocking and Expectations - Morioh
Nock. HTTP server mocking and expectations library for Node.js ... In order to stop recording you should call nock.restore() and recording will stop....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This currently works for me:
You’ve managed to expertly answer my weirdly worded question, so well done. That’s all making sense now. I’ll play around with it a bit more and see what works out well and submit a PR for that readme portion.
Thanks again