Skip to content

seebigs/require-cache-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

require-cache-mock

Requiring this package will allow you to mock modules from anywhere in your node project

Combine this feature with bundl-pack to get the same results in a browser

Uses mock-require under the hood to facilitate mocking

Note: Using this package modifies your global require

Install

$ npm install require-cache-mock

Use

Where replacement is an object or function to return in place of the original module:

require.cache.mock('./module.js', replacement);

Example

require('require-cache-mock');

var entry = require('./entry.js');

entry.init();

// two is mocked

entry.js

require.cache.mock('./two.js', { type: 'mocked' });

function init () {
    var one = require('./one.js');
    console.log('two is ' + one.twoType);
}

module.exports = { init: init };

one.js

var two = require('./two.js');

module.exports = {
    twoType: two.type
};

two.js

module.exports = {
    type: 'real'
};

Stop Mocking

require.cache.mock.stopAll();

Additional Options

See mock-require for more details

require.cache.mock === require('mock-require');

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published