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.

Tests aren't cleaning up nock scope correctly

See original GitHub issue

Noticed while testing https://github.com/node-nock/nock/pull/704:

  1. On that PR’s branch, check out be1533746bc728bf3f136aaf9924aa10e7031bf5.
  2. Run ./node_modules/.bin/tap --harmony -R spec ./tests/test_intercept.js. It’ll fail on query() will not match when the path has no query.
  3. Comment out the tests above it, lines 28-4844, and run the test again – now it passes.
  4. Un-comment the tests, add nock.cleanAll(); on line 4847, and run it again – it still passes.

This suggests to me that something is leaking across the tests. I’m not familiar with the tap library, but I don’t see any beforeEach or afterEach mechanism in its docs.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
erichulburdcommented, Mar 13, 2017

I’ve tried numerous different setup and teardown strategies and cannot get multiple tests to run properly with nock (running mocha and supertest). For instance:

  beforeEach(()=>{
    nock.disableNetConnect();
    nock.enableNetConnect(/^(127\.0\.0\.1|localhost)/);
  });
  afterEach(()=>{
    nock.cleanAll();
    nock.restore();
  });
  it('works on the first run', async ()=>{
   const scopes  = nock.load('./scopes1.json');
   await doSomething();
   expect(scopes.every(scope=>scope.isDone()); // true
  });
  it('does not work on the second run', async ()=>{
   const scopes  = nock.load('./scopes1.json');
   await doSomething();
   expect(scopes.every(scope=>scope.isDone()); // false
  });

package.json:

    "nock": "9.0.x",
    "mocha": "3.2.x",
4reactions
StarpTechcommented, Jun 7, 2017

I also faced with this issue. I clean up all mocks with

  afterEach((done) => {
    Nock.cleanAll()
    Nock.restore()
    done()
  })

and in every test I mock the same endpoint.

Test 1 respond with 500 Test 2 respond with 200

but now I get 500 in Test 2 because Nock.pendingMocks() still contains two entrys 😦

nock: 9.0.13 NodeJs 8

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reset pendingMocks on nock scope - node.js - Stack Overflow
I'm trying to run multiple assertions against a nock scope and mock the ... pendingMocks() with requests even after I've cleaned all with:...
Read more >
Testing Code | Lecstor's Blog
In tests, call isDone on each nock scope before checking the result. If you check the result first, and the nock scope after,...
Read more >
How to Test External APIs with Nock - The New Stack
The most thorough way is to spin up the service locally using a Docker image of it. Granted, the setup is a little...
Read more >
nock reset between tests | The AI Search Engine You Control
There is a way to do before hooks in tap, however it appears to require nesting the tests. Another advantage of resetting state...
Read more >
nock - npm
On your test, you can setup your mocking object like this: const nock = require('nock') const scope = nock('https://api.github.com') ...
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