From f7849683506c4b3fe48c4f1105b216ce6519be7a Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Wed, 21 Apr 2021 13:37:56 +1000 Subject: [PATCH] Cleanup temporal module from a cache, fixes #174 --- lib/proxyquire.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/proxyquire.js b/lib/proxyquire.js index 64dca95..e6bcf69 100644 --- a/lib/proxyquire.js +++ b/lib/proxyquire.js @@ -284,6 +284,13 @@ Proxyquire.prototype._disableModuleCache = function (path, module) { // Return a function that will undo what we just did return function () { + const currentModule = Module._cache[id]; + const currentModuleParent = currentModule.parent; + // remove reference to a temporal module from a parent + if (currentModuleParent && currentModuleParent.children) { + currentModuleParent.children = currentModuleParent.children.filter(m => m !== currentModule) + } + // restore cache entry if (cached) { Module._cache[id] = cached } else {