JSPM

  • Created
  • Published
  • Downloads 88799
  • Score
    100M100P100Q173845F
  • License MIT

Makes it easy to replace require'd dependencies.

Package Exports

  • quibble

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (quibble) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

quibble

Quibble is sorta like proxyquire, sandboxed-module and mockery. Using quibble you can replace how require will behave for a given path, with its intended use being almost solely unit testing.

Usage

Say we're testing pants:

quibble = require('quibble')

describe('pants', function(){
  var subject, legs;
  beforeEach(function(){
    legs = quibble('./../lib/legs', function(){ return 'ooh, legs';});

    subject = require('./../lib/pants');
  });
  // ... more test stuff
});

That way, when the subject loaded from lib/pants runs require('./legs'), it will get back the function that returns 'ooh, legs'.

Configuration

There's only one option: what you want to do with quibbled modules by default.

Say you're pulling in testdouble.js and you want every quibbled module to default to a single test double function with a name that matches its absolute path. You could do this:

quibble = require('quibble')
beforeEach(function(){
  quibble.config({
    defaultFakeCreator: function(path) {
      return require('testdouble').create(path);
    }
  });
});

With this set up, running quibble('./some/path') will default to replacing all require('../anything/that/matches/some/path') invocations with a test double named after the absolute path resolved to by './some/path'.

Spiffy!

How's it different?

A few things that stand out about quibble:

  1. No partial mocking, as proxyquire does. Partial Mocks are often seen problematic and not helpful for unit testing designed to create clear boundaries between the SUT and its dependencies
  2. Global replacements, so it's easy to set up a few arrange steps in advance of instantiating your subject (using require just as you normally would). The instantiation style of other libs is a little different (e.g. require('./my/subject', {'/this/thing': stub})
  3. Require strings are resolved to absolute paths. It can be a bit confusing using other tools because from the perspective of the test particular paths are knocked out from the perspective of the subject and not from the test listing, which runs counter to how every other Node.js API works. Instead, here, the path of the file being knocked out is relative to whoever is knocking it out.
  4. A configurable default faker function. This lib was written with testdouble.js in mind, thinking that you'd want to default to just create a new testdouble and return it in each case by default, without having to manually create it and pass it in (for somewhat more minimal test setup)
  5. A reset() method that undoes everything, intended to be run afterEach test runs