Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cuculus

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cuculus

Simplest require mocking

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

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.

License

MIT © Sergey Kamardin

Keywords

FAQs

Package last updated on 17 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc