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 |
|
9 | 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.
|
10 |
|
11 | ##Usage
|
12 |
|
13 | ```javascript
|
14 | var mock = require('mock-require');
|
15 |
|
16 | mock('http', { request: function() {
|
17 | console.log('http.request called');
|
18 | }});
|
19 |
|
20 | var http = require('http');
|
21 | http.request(); // 'http.request called'
|
22 | ```
|
23 | ##API
|
24 |
|
25 | ```javascript
|
26 | mock(path, mockExport)
|
27 | ```
|
28 |
|
29 | `path`: `String`
|
30 |
|
31 | The module you that you want to mock. This is the same string you would pass in if you wanted to `require` the module.
|
32 |
|
33 | This 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 |
|
37 | The 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 |
|
41 | The 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 |
|
45 | The 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
|
48 | require('fs', 'path');
|
49 | require('fs') === require('path'); // true
|
50 | ```
|
51 | This 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
|
55 | module.exports = function() {
|
56 | return 'this was mocked';
|
57 | };
|
58 | ```
|
59 |
|
60 | `test/a_spec.js`:
|
61 | ```javascript
|
62 | var mock = require('mock-require');
|
63 | mock('../some/dependency', './spy');
|
64 | ...
|
65 | ```
|
66 |
|
67 | `test/b_spec.js`:
|
68 | ```javascript
|
69 | var mock = require('mock-require');
|
70 | mock('../some/other/dependency', './spy');
|
71 | ...
|
72 | ```
|
73 |
|
74 | ```javascript
|
75 | mock.stop(path)
|
76 | ```
|
77 |
|
78 | `path`: `String`
|
79 |
|
80 | The 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 |
|
82 | This 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
|
85 | var mock = require('mock-require');
|
86 | mock('fs', { mockedFS: true });
|
87 |
|
88 | var fs1 = require('fs');
|
89 |
|
90 | mock.stop('fs');
|
91 |
|
92 | var fs2 = require('fs');
|
93 |
|
94 | fs1 === fs2; // false
|
95 | ```
|
96 |
|
97 | ##Test
|
98 | npm test
|