Giter VIP home page Giter VIP logo

mock-require's Introduction

#mock-require

####Simple, intuitive mocking of Node.js modules.

Build Status

##About

mock-require is useful if you want to mock require statements in Node.js. I wrote it because I wanted something with a straight-forward API that would let me mock anything, from a single exported function to a standard library.

##Usage

var mock = require('mock-require');

mock('http', { request: function() {
  console.log('http.request called');
}});

var http = require('http');
http.request(); // 'http.request called'

##API

###mock(path, mockExport)

path: String

The module you that you want to mock. This is the same string you would pass in if you wanted to require the module.

This path should be relative to the current file, just as it would be if you were to require the module from the current file. mock-require is smart enough to mock this module everywhere it is required, even if it's required from a different file using a different relative path.

mockExport : object/function

The function or object you want to be returned from require, instead of the path module's exports.

mockExport : string

The module you want to be returned from require, instead of the path module's export. This allows you to replace modules with other modules. For example, if you wanted to replace the fs module with the path module (you probably wouldn't, but if you did):

mock('fs', 'path');
require('fs') === require('path'); // true

This is useful if you have a mock library that you want to use in multiple places. For example:

test/spy.js:

module.exports = function() {
    return 'this was mocked';
};

test/a_spec.js:

var mock = require('mock-require');
mock('../some/dependency', './spy');
...

test/b_spec.js:

var mock = require('mock-require');
mock('../some/other/dependency', './spy');
...

###mock.stop(path)

path: String

The module you that you want to stop mocking. This is the same string you would pass in if you wanted to require the module.

This will only modify variables used after mock.stop is called. For example:

var mock = require('mock-require');
mock('fs', { mockedFS: true });

var fs1 = require('fs');

mock.stop('fs');

var fs2 = require('fs');

fs1 === fs2; // false

###mock.stopAll()

This function can be used to remove all registered mocks without the need to remove them individually using mock.stop().

mock('fs', {});
mock('path', {});

var fs1 = require('fs');
var path1 = require('path');

mock.stopAll();

var fs2 = require('fs');
var path2 = require('path');

fs1 === fs2; // false
path1 === path2; // false

##Test

npm test

mock-require's People

Contributors

boblauer avatar fuqcool avatar josh-egan avatar mingos777 avatar

Watchers

 avatar  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.