Skip to content

Commit

Permalink
Clean up proxyquired module after restoring cache
Browse files Browse the repository at this point in the history
If a module is loaded for the first time via proxyquire, it should be deleted from cache after loading. Previously we were only capturing an existing cached module and restoring it after loading.
  • Loading branch information
jwalton authored and bendrucker committed Mar 19, 2018
1 parent d3a155d commit 851baaa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/proxyquire.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ Proxyquire.prototype._disableModuleCache = function (path, module) {
return function () {
if (cached) {
Module._cache[id] = cached
} else {
delete Module._cache[id]
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/proxyquire-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ describe('Proxyquire', function () {
assert.equal('cached', foo.state)
assert.equal(foo, original)
})

it('does not pollute the cache when module is proxyquired before it is loaded', function () {
var proxyquire = require('..')

proxyquire('./samples/no-call-thru-test', { './required': false })
var original = require('./samples/no-call-thru-test')

assert.equal(original.original, true)
})
})

describe('preserveCache()', function () {
Expand Down
1 change: 1 addition & 0 deletions test/samples/no-call-thru-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.original = require('./required')
1 change: 1 addition & 0 deletions test/samples/no-call-thru-test/required.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = true

0 comments on commit 851baaa

Please sign in to comment.