0.3.3 • Published 9 years ago

cuculus v0.3.3

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

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

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 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