cuculus

0.3.3 • Public • Published

NPM version Build Status Dependency Status

Simplest require mocking

Whats up

Hey there!

This is a simple module mocking tool for your super clever unit testing.

Install

$ npm install --save-dev cuculus

Usage

The simple case:

    var cuculus = require("cuculus");
 
    cuculus.replace("fs", {
        writeFile: function(path, contents) {
            // ...
        }
    });
 
    // now fs is your object
 
    cuculus.restore("fs");
 
    // now fs is original

Or:

    var cuculus = require("cuculus"),
        restorer;
 
    restorer = cuculus.replace("fs", {
        writeFile: function(path, contents) {
            // ...
        }
    });
 
    // now fs is your object
 
    restorer();
 
    // now fs is original

Complete case:

var cuculus = require("cuculus");
// mocking library
// feel free to use your favorite
var sinon = require("sinon");
 
cuculus.modify("fs", function(fs, onRestore) {
    var stub;
 
    stub = sinon.stub(fs, "writeFile", function(path, contents) {
        // your actions here
    });
 
    // register restore middleware
    onRestore.push(stub.restore.bind(stub));
 
    return fs;
});
 
// your tests here
 
// and after all, or, between each test
cuculus.restore("fs");
 
// now fs is native and without stubs

API

cuculus.replace(name: string, stub: Any) : Function()

Complete replace the module named name with stub. Returns function, that simple proxy to cuculus.restore(name).

cuculus.modify(name: string, replacer: Function(current: Any, onRestore: Function(fn: Function))) : Function()

Modifies current module with replacer function. If replacer modifies object, then restore method will not restore the changes, until you not register the backupers with onRestore function.

cuculus.restore(name: string[, steps: number])

Restores module name. If it was modified multiple times, restores to the root, until the steps limit is not given.

cuculus.drop(name: string)

Drops the cached module from require. name should be a module name or a full path to the js file.

License

MIT © Sergey Kamardin

Package Sidebar

Install

npm i cuculus

Weekly Downloads

0

Version

0.3.3

License

MIT

Last publish

Collaborators

  • gobwas