quibble
Makes it easy to replace require'd dependencies.
Last updated 8 years ago by searls .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install quibble 
SYNC missed versions from official npm registry.

quibble

Build Status

Quibble is a terser (and more magical) alternative to packages like proxyquire, sandboxed-module and mockery for mocking out dependencies in tests of Node.js modules. Using quibble you can replace how require() will behave for a given path. Its intended use is squarely focused on unit testing. It is almost-but-not-quite a private dependency of testdouble.js, as it implements the td.replace() function's module-replacement behavior.

Usage

Say we're testing pants:

quibble = require('quibble')

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

    subject = require('./../lib/pants');
  });
  it('contains legs', function() {
    expect(subject().left).toContain('a leg')
    expect(subject().right).toContain('a leg')
  })
});

That way, when the subject loaded from lib/pants runs require('./legs'), it will get back the function that returns 'a leg'. The fake value is also returned by quibble, which makes it easy to set and assign a test double in a one-liner.

For more info on how this module is really intended to be used, check out its inclusion in testdouble.js

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!

Note: defaultFakeCreator is not supported for ES Module stubbing

ES Modules support

Quibble supports ES Modules. Quibble implements ES module support using ES Module Loaders which are the official way to "patch" Node.js' module loading mechanism for ESM.

Note that Loader support is currently experimental and unstable. We will be doing our best to track the changes in the specification for the upcoming Node.js versions. Also note that Quibble ESM support is tested only for versions 13 and above.

To use Quibble support, you must run Node with the quibble package as the loader:

node --loader=quibble ...

Most test runners allow you to specify this in their command line, e.g. for Mocha:

mocha --loader=quibble ...

The quibble loader will enable the replacement of the ES modules with the stubs you specify, and without it, the stubbing will be ignored.

Restrictions on ESM

  • defaultFakeCreator is not yet supported.

quibble ESM API

The API is similar to the CommonJS API, but uses quibble.esm function, and is async:

// a-module.mjs (ESM)
export const life = 42;
export default 'universe';

// uses-a-module.mjs
import universe, {life} from './a-module.mjs';

console.log(life, universe);

(async function () {
  await quibble.esm('./a-module.mjs', {life: 41}, 'replacement universe');

  await import('./uses-some-module.mjs');
  // ==> logs: 41, replacement universe
})();

The parameters to be given to quibble.esm for ESM modules are:

  1. the module path: similar to CommonJS, the path is relative to the directory you are in. It is resolved the ESM way, so if you're using a relative path, you must specify the filename, including the extension.
  2. the named export stubs: either null/undefied or an object with each property having key corresponding to export names and value being the implementation to use. To define the default export you can either define a default property here or use the third argument, but not both at same time.
  3. the default export stub: if named export stubs does not contain a default key, you can define the default stub with this argument.

Note that quibble.reset works the same as for CommonJS modules

ESM support also exposes the function quibble.esmImportWithPath which both imports a module and resolves the path to the module that is the package's entry point:

  • async quibble.esmImportWithPath(importPath): imports a module, just like import(importPath), but returns an object with two properties:
    • module: the module returned by await import(importPath).
    • modulePath: the full path to the module (file) that is the entry point to the package/module.

Note that when mocking internal Node.js modules (e.g. "fs")), you need to mock the named exports both as named exports and as properties in the default export, because Node.js exports internal modules both as named exports and as a default object. Example:

const fsExports = {
  readFileSync: function (path) {
    console.log("using quibbled readFileSyns... yay!");
    return "Looks like 'fs' was replaced correctly.";
  },
}
await quibble.esm("fs", fsExports, fsExports);

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 to support the testdouble.js feature td.replace(), in an effort to reduce the amount of per-test friction to repetitively create & pass in test doubles
  5. A reset() method that undoes everything, intended to be run afterEach test runs

Current Tags

  • 0.6.17                                ...           latest (2 years ago)

33 Versions

  • 0.6.17                                ...           2 years ago
  • 0.6.16                                ...           2 years ago
  • 0.6.15                                ...           2 years ago
  • 0.6.14                                ...           2 years ago
  • 0.6.13                                ...           2 years ago
  • 0.6.12                                ...           2 years ago
  • 0.6.11                                ...           2 years ago
  • 0.6.10                                ...           2 years ago
  • 0.6.9                                ...           3 years ago
  • 0.6.8                                ...           3 years ago
  • 0.6.7                                ...           3 years ago
  • 0.6.6                                ...           3 years ago
  • 0.6.5                                ...           4 years ago
  • 0.6.4                                ...           4 years ago
  • 0.6.3                                ...           5 years ago
  • 0.6.2                                ...           5 years ago
  • 0.6.1                                ...           5 years ago
  • 0.6.0                                ...           5 years ago
  • 0.5.7                                ...           5 years ago
  • 0.5.6                                ...           5 years ago
  • 0.5.5                                ...           7 years ago
  • 0.5.3                                ...           7 years ago
  • 0.5.2                                ...           7 years ago
  • 0.5.1                                ...           8 years ago
  • 0.5.0                                ...           8 years ago
  • 0.4.1                                ...           8 years ago
  • 0.4.0                                ...           8 years ago
  • 0.3.1                                ...           8 years ago
  • 0.3.0                                ...           9 years ago
  • 0.2.1                                ...           9 years ago
  • 0.2.0                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
  • 0.0.1                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 8
Last Day 0
Last Week 8
Last Month 0
Dependencies (1)
Dev Dependencies (5)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |