felixge / node-sandboxed-module

A sandboxed node.js module loader that lets you inject dependencies into your modules.
MIT License
342 stars 42 forks source link

How to mock modules which exports a function as "class" like approach? #32

Closed rogah closed 10 years ago

rogah commented 10 years ago

I'm trying to mock the https://github.com/mscdex/node-ftp which is designed to create a new instance of a function ("class" constructor design) like the following:

// my-module.js
var Client = require('ftp');

exports.doSomething = function() {
    ...
    var c = new Client();
    ...
}

How to achieve this with sandboxed-module?

domenic commented 10 years ago

You should substitute your own class for ftp, e.g.

sandboxedModule.require("./my-module", {
  ftp: MyFtpClientMock
});