question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In Jest, `nock.restore()` prevents subsequent mocks from working

See original GitHub issue

What 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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
drianoazcommented, Oct 1, 2020

This currently works for me:

beforeEach(() => {
  if (!nock.isActive()) {
    nock.activate();
  }
});

afterEach(() => {
  nock.restore();
});
1reaction
arondubycommented, Jul 24, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found