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) {
}
});
cuculus.restore("fs");
Or:
var cuculus = require("cuculus"),
restorer;
restorer = cuculus.replace("fs", {
writeFile: function(path, contents) {
}
});
restorer();
Complete case:
var cuculus = require("cuculus");
var sinon = require("sinon");
cuculus.modify("fs", function(fs, onRestore) {
var stub;
stub = sinon.stub(fs, "writeFile", function(path, contents) {
});
onRestore.push(stub.restore.bind(stub));
return fs;
});
cuculus.restore("fs");
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