Globally override a module that returns a function
See original GitHub issueI know that you can set '@global': true
in the stubs for modules that are objects, but how do you do it for modules that return functions. For example:
// main.js
var Deep = require('./deep');
var instance = new Deep();
instance.run();
module.exports = instance;
// deep.js
var deepest = require('./deepest')('My Name');
function Deep() {
}
Deep.prototype = {
run: function() {
deepest.baz();
}
};
module.exports = new Deep();
// deepest.js
function Deepest(name) {
return {
baz: function() {
console.log(name);
}
};
}
module.exports = Deepest;
Deepest returns a function that I need to mock out, but since it is used inside of Deep, I have to use @global
.
If deepest was exporting the inner object instead of the wrapping function, I could do this:
var main = proxyquire('./main.js', {
'./deepest': {
'@global': true,
'baz: function() {}
}
}
But since deepest returns a function, my understanding is that I have to mock it like this:
var main = proxyquire('./main.js', {
'./deepest': function() {
return {
baz: function() {
console.log('mocked');
}
}
}
}
But I can no longer set ‘@global’ on the mock object. Right? How should I handle this? This situation has arisen because of the api design of a third party module so I sadly can’t really restructure these calls.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Override globals in function imported from another module
My goal is to use the functionality of a.x in b , but change the value returned by the function. Specifically, value will...
Read more >Override function of a Node.js module - Tutorial Kart
The first step to override a function in a module is to include the module itself using require function. var newMod = require('<module_name>');....
Read more >How to Override Underlying JavaScript Modules? - Medium
Imagine your module returns some database records that you want the ids for and your file is called get-database-data. js . module. exports...
Read more >How to override a global function? - Odoo
In order to override this global method you need to follow below steps,. Define one global function into you custom module. from osv...
Read more >How to override functions of module in Node.js - GeeksforGeeks
Overriding means not change the original definition of any object or module but changing its behavior in the current scenario (where it has ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sure you can! Just assign create a function assigned to a variable and then set
deepest['@global'] = true
.@bendrucker This
'@global' = true
implementation of yours is truly genius! 👍