Skip to content

Commit

Permalink
fix: no undefined entries in require.cache from modules to keep confi…
Browse files Browse the repository at this point in the history
…guration

fixes #4
  • Loading branch information
analog-nico committed May 9, 2017
1 parent 3219fc7 commit 773c50a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/index.js
Expand Up @@ -68,7 +68,9 @@ module.exports = function (requireCache, callback, callbackForModulesToKeep, mod
if (callbackForModulesToKeep) {
// In case modules to keep were required inside the stealthy require for the first time, copy them to the restored cache
for ( var k = 0; k < modulesToKeep.length; k+=1 ) {
requireCache[modulesToKeep[k]] = stealthCache[modulesToKeep[k]];
if (stealthCache[modulesToKeep[k]]) {
requireCache[modulesToKeep[k]] = stealthCache[modulesToKeep[k]];
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/spec/non-native.js
Expand Up @@ -99,4 +99,30 @@ describe('When only non-native modules are involved, Stealthy-Require', function

});

it('should not pollute require cache with dependencies that should be kept but are never required', function () {

var testReq1 = require('../fixtures/sync-deps.js');
var testReq2 = require('../fixtures/no-deps.js');
delete require.cache[require.resolve('../fixtures/sync-deps.js')];
delete require.cache[require.resolve('../fixtures/no-deps.js')];
var testReq3 = require('../fixtures/sync-deps.js');
var testReq4 = require('../fixtures/no-deps.js');
expect(testReq1).to.not.eql(testReq3);
expect(testReq2).to.not.eql(testReq4);

delete require.cache[require.resolve('../fixtures/sync-deps.js')];
delete require.cache[require.resolve('../fixtures/no-deps.js')];

stealthyRequire(require.cache, function () {
return require('../fixtures/no-deps.js');
},
function () {
require('../fixtures/sync-deps.js');
}, module);

expect(require.cache[require.resolve('../fixtures/sync-deps.js')]).to.eql(undefined);
expect(require.cache.hasOwnProperty(require.resolve('../fixtures/sync-deps.js'))).to.eql(false); // eslint-disable-line no-prototype-builtins

});

});

0 comments on commit 773c50a

Please sign in to comment.