----------- UPDATE ---------------
node-sandbox works on the same principles as below, but ends in a good module. I am very pleased to work with.
--------------- detailed awnser ---------------
After much testing, I found the best way to test node modules in isolation, while the mocking thing is to use the Vojta Jina method to run each module inside vm with a new context, as described here .
with this vm testing module:
var vm = require('vm'); var fs = require('fs'); var path = require('path'); exports.loadModule = function(filePath, mocks) { mocks = mocks || {};
you can test each module with its own context and easily drown out all external dependencies.
fsMock = mocks.createFs(); mockRequest = mocks.createRequest(); mockResponse = mocks.createResponse(); // load the module with mock fs instead of real fs // publish all the private state as an object module = loadModule('./web-server.js', {fs: fsMock});
I highly recommend this method for writing effective tests in isolation. Only acceptance tests should hit the entire stack. Unit and integration tests should test individual parts of the system.
source share