0

Is it possible to mock an anonymous function using proxyquire with Node.js? I have a module I need to mock whose constructor is an anonymous class. Is it possible to use proxyquire to mock this, or is there another way to do this?

The reason I'm using proxyquire is because it's a module that is 'required' by the file I'm writing unit tests for.

An example of the code and the function that is returned is:

var example = require('example')(config);

This returns an anonymous function that takes in config as parameters.

function (config) {
    return new Example(config);
}
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
arjun
  • 103
  • 1
  • 10
  • 1
    What is the end result you're after? – sg.cc Jan 29 '16 at 22:41
  • I'm trying to use mock some of the functions that this example library has. If I use something like proxyquire to take control of the require statement and use my mocked module instead of the original, I need to find a way to mock the anonymous function as well. – arjun Jan 30 '16 at 03:18
  • Can you add a slightly longer code example of how you use the required module in your code. To do this properly you are going to need to refactor your code a bit to use dependency injection, but I'll need a bit more code to show you how to do that. – Andrew Eddie Feb 05 '16 at 03:21

0 Answers0