proxyquire: Proxyquire is leaking on module.parent

This one took me a while to figure out, had to write my own “slim” proxyquire to find it.

Where I work, we use proxyquire to mock our modules and our test suite has around 1050 tests. We ran into issues where mocha --watch would crash after a couple of runs because the garbage collector was not able to garbage collect anymore.

If I compare successive heap dumps I notice that as time goes by I end up having more and more Module objects.

image

If you look at the contructor of Module, you’ll notice that whenever we load a new module, this one gets pushed as a children of the module’s parent.

In my slim proxyquire, the fix for the leak was to simply parent.children.pop() whenever I call Module._load and to also remove proxyquire from proxyquire’s parent module.children.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Sounds like this is not a problem for proxyquire, but for any other “nodejs” mocking library, as long they all work relatively the same.

@rochdev, we ended up using my rewrite for our project. You could either use that or write your own. It wasn’t too bad to do. It turns out to about 140 LOC.

Great resources: https://nodejs.org/api/modules.html#modules_caching http://fredkschott.com/post/2014/06/require-and-the-module-system/ https://github.com/charlespwd/proxyquire/blob/master/src/proxyquire.js

@charlespwd Any plans to release it on npm?