https://github.com/seebigs/require-cache-mock
https://github.com/seebigs/require-cache-mock
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/seebigs/require-cache-mock
- Owner: seebigs
- Created: 2017-01-20T00:27:29.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-20T03:31:27.000Z (over 9 years ago)
- Last Synced: 2026-03-25T09:22:44.087Z (3 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# require-cache-mock
> Requiring this package will allow you to mock modules from anywhere in your node project
Combine this feature with [bundl-pack](https://github.com/seebigs/bundl-pack) to get the same results in a browser
Uses [mock-require](https://github.com/boblauer/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:*
```js
require.cache.mock('./module.js', replacement);
```
## Example
```js
require('require-cache-mock');
var entry = require('./entry.js');
entry.init();
// two is mocked
```
entry.js
```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
```js
var two = require('./two.js');
module.exports = {
twoType: two.type
};
```
two.js
```js
module.exports = {
type: 'real'
};
```
## Stop Mocking
```js
require.cache.mock.stopAll();
```
## Additional Options
See [mock-require](https://github.com/boblauer/mock-require) for more details
```js
require.cache.mock === require('mock-require');
```