Giter Club home page Giter Club logo

fraudster's Introduction

#fraudster

Simple mock injection for Node require statements

Fraudster is not a mocking framework, it just allows you to inject your mock / fakes / stubs inplace of what would usualy be loaded by require calls.

Why

I have been using mockery heavily for some time.

I like how it works, but have always wanted to change a few things it seams to be not maintained any more.

The main thing I needed merged was making it instanceable so that mocks do not leak into other tests...

This module is heavily based on that code but I have simplified a few things.

Install

npm i fraudster

Usage

var Fraudster = require('fraudster'),
    fraudster = new Fraudster(),
    fakeFs = {
        exists : function(path, callback){
            // Calls to fs.exists will always return true
            callback(null, true);
        }
    };

fraudster.registerMock('fs', fakeFs);

var fs = require('fs'); // Real fs module

fraudster.enable();

var fakeFs = require('fs'); // Fake fs module

fraudster.disable();

Options

When constructing Fraudster you can pass an optional options object to enable / disable warnings and errors

var Fraudster = require('fraudster'),
    fraudster = new Fraudster({
        warnOnUnregistered: true,
        errorOnUnregistered: true,
        warnOnReplace: true,
        errorOnReplace: true
    });

If warn is enabled a warning will be logged to the console, if error is enabled an Error will be thrown

Registering mocks

Register mocks to make them available when Fraudster is enabled

var fakeFs = {
    exists : function(path, callback){
        // Calls to fs.exists will always return true
        callback(null, true);
    }
};

fraudster.registerMock('fs', fakeFs);

If you no longer want your mock to be used, you can deregister it:

fraudster.deregisterMock('fs');

Allowable modules

If you enable Fraudster load a module that has not been mocked, Fraudster will by default log a warning to the console. This is so that you don't inadvertently use downstream modules without being aware of them. By registering a module as "allowable", you tell Fraudster that you know about its use, and are happy for the original module to be loaded.

The most common use case for this is your source-under-test, which obviously you'll want to load without warnings. For example:

fraudster.registerAllowable('./my-source-under-test');

You can also register many allowables at once with registerAllowables

fraudster.registerAllowables(['./my-source-under-test', './someOtherModule']);

If you later want to remove an allowable this can be done as below

fraudster.deregisterAllowable('./my-source-under-test');

or

fraudster.deregisterAllowables(['./my-source-under-test', './someOtherModule']);

Deregistering everything

If you just want to destoy all the things you can use the deregisterAll method

fraudster.deregisterAll();

fraudster's People

Contributors

mauricebutler avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar Milton Sosa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.