question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Globally override a module that returns a function

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bendruckercommented, Mar 25, 2015

Sure you can! Just assign create a function assigned to a variable and then set deepest['@global'] = true.

var deepest = function () {}
deepest['@global'] = true
0reactions
MarkHerholdcommented, Aug 6, 2016

@bendrucker This '@global' = true implementation of yours is truly genius! 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found