Skip to content

Commit

Permalink
Fix for certain child modules not getting cleared (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregj committed Nov 3, 2021
1 parent b4412fe commit 32f6510
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fixture-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = {};
2 changes: 2 additions & 0 deletions fixture-with-dependency.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// eslint-disable-next-line no-unused-vars
const _ = require('./fixture-empty');
const fixture = require('./fixture');

module.exports = () => fixture();
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const clear = moduleId => {

// Remove all descendants from cache as well
if (require.cache[filePath]) {
const {children} = require.cache[filePath];
const children = require.cache[filePath].children.map(child => child.id);

// Delete module from cache
delete require.cache[filePath];

for (const {id} of children) {
for (const id of children) {
clear(id);
}
}
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ test('clearModule() recursively', t => {
t.is(require(id)(), 1);
});

test('clearModule() recursively, multiple imports', t => {
clearModule.all();
const id = './fixture-with-dependency';
t.is(require(id)(), 1);
t.is(require(id)(), 2);
t.is(require(id)(), 3);
clearModule(id);
t.is(require(id)(), 1);
});

test('clearModule.single()', t => {
clearModule.all();
const id = './fixture-with-dependency';
Expand Down

0 comments on commit 32f6510

Please sign in to comment.