Skip to content

Commit 6429764

Browse files
committed
feat: child dependencies will now have their deps mocked too
1 parent 0f52091 commit 6429764

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ module.exports = (url) => {
2121

2222
**test.js**
2323
```js
24+
const request = require('request')
25+
2426
const mockedRequest = (url, options, callback) => {
25-
// Mock a request here.
27+
// Log all requests.
28+
console.log('Request made:', url);
29+
request(url, options, callback)
2630
};
2731

2832
const foo = muk('./foo', {
33+
// Will overwrite all requires of "request" with our own version.
2934
request: mockedRequest
3035
});
3136
```

lib/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ module.exports = (filename, deps) => {
1616

1717
// Load children.
1818
const children = {};
19-
Object.keys(deps).forEach((key) => {
19+
for (let key in deps) {
2020
const childpath = Module._resolveFilename(key, m);
2121
const child = children[childpath] = new Module(childpath, m);
22+
require.cache[childpath] = child;
2223
child.paths = Module._nodeModulePaths(path.dirname(childpath));
2324
child.loaded = true;
2425
child.exports = deps[key];
25-
});
26+
}
2627

2728
m.require = (path) => {
2829
const childpath = Module._resolveFilename(path, m);
@@ -37,8 +38,12 @@ module.exports = (filename, deps) => {
3738
// Load module.
3839
m.load(filename);
3940

40-
// Delete module from cache so it can be required normally.
41+
// Clear so mocked modules can be required normally.
4142
delete require.cache[filename];
43+
for (let key in deps) {
44+
let childpath = Module._resolveFilename(key, m);
45+
delete require.cache[childpath];
46+
}
4247

4348
return m.exports;
4449
};

test/custom/child.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('fs');

test/custom/parent.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs');
2+
const child = require('./child');
3+
4+
5+
module.exports = (callback) => {
6+
process.nextTick(() => {
7+
callback(fs, child);
8+
});
9+
};

test/require-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ describe('Mock required relative file', () => {
5656
describe('Mock required relative file in a different dir', () => {
5757
require('./dir')('./custom', './bar');
5858
});
59+
60+
describe.only('Mock modules required by children too', () => {
61+
it('Mocks dependency in both parent and children', () => {
62+
let mymock = {};
63+
muk('./custom/parent.js', { 'fs': mymock })((fs, child) => {
64+
assert.equal(fs, mymock);
65+
assert.equal(child, mymock);
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)