UNPKG

2.82 kBMarkdownView Raw
1#mock-require
2
3####Simple, intuitive mocking of Node.js modules.
4
5[![Build Status](https://travis-ci.org/boblauer/mock-require.svg)](https://travis-ci.org/boblauer/mock-require)
6
7##About
8
9mock-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.
10
11##Usage
12
13```javascript
14var mock = require('mock-require');
15
16mock('http', { request: function() {
17 console.log('http.request called');
18}});
19
20var http = require('http');
21http.request(); // 'http.request called'
22```
23##API
24
25```javascript
26mock(path, mockExport)
27```
28
29`path`: `String`
30
31The module you that you want to mock. This is the same string you would pass in if you wanted to `require` the module.
32
33This path should be relative to the current file, just as it would be if you were `require`ing 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.
34
35`mockExport` : `function`
36
37The function you want to be returned from `require`, instead of the module's exported function. This is useful if your module only exports a single function.
38
39`mockExport` : `object`
40
41The object you wnat to be returned from `require`, instead of the module's exported object. This is useful if your module exports a complex object, such as `require('fs')`.
42
43`mockExport` : `string`
44
45The module you want to be returned from `require`, instead of the 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 it's just an example):
46
47```javascript
48require('fs', 'path');
49require('fs') === require('path'); // true
50```
51This is useful if you have a mock library that you want to use in multiple places. For example:
52
53`test/spy.js`:
54```javascript
55module.exports = function() {
56 return 'this was mocked';
57};
58```
59
60`test/a_spec.js`:
61```javascript
62var mock = require('mock-require');
63mock('../some/dependency', './spy');
64...
65```
66
67`test/b_spec.js`:
68```javascript
69var mock = require('mock-require');
70mock('../some/other/dependency', './spy');
71...
72```
73
74```javascript
75mock.stop(path)
76```
77
78`path`: `String`
79
80The module you that you want to stop mock. This is the same string you would pass in if you wanted to `require` the module.
81
82This will not modify any modules that have already been required, it will only modify modules required after `mock.stop` is called. For example:
83
84```javascript
85var mock = require('mock-require');
86mock('fs', { mockedFS: true });
87
88var fs1 = require('fs');
89
90mock.stop('fs');
91
92var fs2 = require('fs');
93
94fs1 === fs2; // false
95```
96
97##Test
98npm test