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.

Memory leaked when requiring nock

See original GitHub issue

What is the expected behavior?

Nock should not keep any references that leaks memory.

What is the actual behavior?

Nock leaks memory as soon as it is required in a test.

How to reproduce the issue

Repro files: https://gist.github.com/avaly/8965470dfc5891836e57e2f95933f3cd

$ jest --detectLeaks
 FAIL  ./sum.test.js
  ● Test suite failed to run

    EXPERIMENTAL FEATURE!
    Your test suite is leaking memory. Please ensure all references are cleaned.

    There is a number of things that can leak memory:
      - Async operations that have not finished (e.g. fs.readFile).
      - Timers not properly mocked (e.g. setInterval, setTimeout).
      - Keeping references to the global scope.

      at node_modules/jest-cli/build/TestScheduler.js:240:24

As soon as the require('nock'); line is removed/commented out:

$ jest --detectLeaks
 PASS  ./sum.test.js
  ✓ adds 1 + 2 to equal 3 (2ms)

Versions

Software Version(s)
Nock 10.0.6
Node 8.15.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
eviltwincommented, Dec 12, 2019

just for anyone coming across this today, it’s possible to clear up the leak by putting the following either in your test file or in your jest entry point:

afterAll(() => {
  // we must clean up nock and restore the http module to normal, otherwise
  // the tests will leak memory as it runs each test suite.
  nock.cleanAll()
  nock.restore()
})
2reactions
paulmelnikowcommented, Jul 8, 2019

I’m going to unpin this. Without a minimal test case, or even a very small test case with Jest, it’s hard to act on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Jest + Nock under control
This is just a short tale on how I fixed some of the memory leaking issues we can face with Jest and Nock,...
Read more >
Your Jest Tests are Leaking Memory
Why does Jest leak memory? Jest resets the require cache between each test run. This is really nice because it gives test suites...
Read more >
Calling nock.restore() after each test suite doesn't stop ...
Calling nock.restore() after each test suite doesn't stop memory leak.
Read more >
Memory leak - Wikipedia
In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in...
Read more >
Would this be considered a memory leak? - Stack Overflow
@Nick: The source of the memory (stack, heap, static) is irrelevant to the OS--it's all just memory, and it will be replaced and...
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