node package manager


Unit-testing helper to replace "require" in tested code


Stubble is a tool for node.js to help aid in your unit testing. Stubble allows you to require your source files in such a way that you can stub out any requirements you choose to.


npm install stubble


Let's say you have a simple server script, server.js:

var http = require('http');

server = http.createServer(function(req, res) {
  res.end('Hello World!');

server.listen(3000, '');
module.exports = server;

Obviously, this could be tricky to have written in a TDD fashion, as simply requiring this script in your test would not work well. Stubble to the rescue! First, create a new Stubble instance with a hash of any modules you want Stubble to intercept. Assuming you are writing your tests in Jasmine, your spec might start by looking something like this:

var Stubble  = require('stubble'),
    req      = {},
    res      = {
      end: jasmine.createSpy('res.end')
    server   = {
      listen: jasmine.createSpy('server.listen')
    httpMock = {
      createServer: jasmine.createSpy('http.createServer').andReturn(server)
    stub = new Stubble(http: httpMock);

Now that you have your Stubble instance, you can use this to require your module and make assertions against it.

var returned = stub.require('./server');
describe('server', function() {

  it('creates a server', function() {

  it('calls res.end', function() {
    http.createServer.mostRecentCall.args[0](req, res);
    expect(res.end).toHaveBeenCalledWith('Hello World!');

  it('starts the server', function() {
    expect(server.listen).toHaveBeenCalledWith(3000, '');

  it('exports the server', function() {

You can even make specific assertions about the require calls that were made.



Testing this package requires the global jasmine-node module.