Using proxyquire for stub required depending on third-party modules

I am trying to use proxyquire to silence a request in a module that is required by a third-party module.

Example:

My module requires a 3rd mod called "foo".

This module depends on another library called "bar", and there is a need in the bar that I want to make fun of. Is it possible?

in the third party lib is called "three":

  var bar = require('bar');

in bar lib:

  var thingiwanttomock = require('thingiwanttomock');

Then something like this in my test:

  it("test", function() {
    var mocked = proxyquire('thingiwanttomock', {});
  });

EDIT:

I think I want something like this:

var three = proxyquire('three', {
  'bar': {
    'thingiwanttomock': {
       'mocked': true 
     }
    }
  }
});

However, if I put the console log in the bar library and print out that the thingiwanttomock variable after the request is not my mocked object.

in bar lib:

  var thingiwanttomock = require('thingiwanttomock');
  // this is not my object object
  console.log('thingiwanttomock should be a mock', thingiwanttomock);

proxyquire , require lib? , .

+4

Source: https://habr.com/ru/post/1624179/


All Articles