1 | 'use strict';
|
2 |
|
3 | const assert = require('assert');
|
4 | const mock = require('..');
|
5 |
|
6 | describe('Mock Require', () => {
|
7 | afterEach(() => {
|
8 | mock.stopAll();
|
9 | });
|
10 |
|
11 | it('should mock a required function', () => {
|
12 | mock('./exported-fn', () => {
|
13 | return 'mocked fn';
|
14 | });
|
15 |
|
16 | assert.equal(require('./exported-fn')(), 'mocked fn');
|
17 | });
|
18 |
|
19 | it('should mock a required object', () => {
|
20 | mock('./exported-obj', {
|
21 | mocked: true,
|
22 | fn: function() {
|
23 | return 'mocked obj';
|
24 | }
|
25 | });
|
26 |
|
27 | let obj = require('./exported-obj');
|
28 | assert.equal(obj.fn(), 'mocked obj');
|
29 | assert.equal(obj.mocked, true);
|
30 |
|
31 | mock.stop('./exported-obj');
|
32 |
|
33 | obj = require('./exported-obj');
|
34 | assert.equal(obj.fn(), 'exported object');
|
35 | assert.equal(obj.mocked, false);
|
36 | });
|
37 |
|
38 | it('should unmock', () => {
|
39 | mock('./exported-fn', () => {
|
40 | return 'mocked fn';
|
41 | });
|
42 |
|
43 | mock.stop('./exported-fn');
|
44 |
|
45 | const fn = require('./exported-fn');
|
46 | assert.equal(fn(), 'exported function');
|
47 | });
|
48 |
|
49 | it('should mock a root file', () => {
|
50 | mock('.', { mocked: true });
|
51 | assert.equal(require('.').mocked, true);
|
52 | });
|
53 |
|
54 | it('should mock a standard lib', () => {
|
55 | mock('fs', { mocked: true });
|
56 |
|
57 | const fs = require('fs');
|
58 | assert.equal(fs.mocked, true);
|
59 | });
|
60 |
|
61 | it('should mock an external lib', () => {
|
62 | mock('mocha', { mocked: true });
|
63 |
|
64 | const mocha = require('mocha');
|
65 | assert.equal(mocha.mocked, true);
|
66 | });
|
67 |
|
68 | it('should one lib with another', () => {
|
69 | mock('fs', 'path');
|
70 | assert.equal(require('fs'), require('path'));
|
71 |
|
72 | mock('./exported-fn', './exported-obj');
|
73 | assert.equal(require('./exported-fn'), require('./exported-obj'));
|
74 | });
|
75 |
|
76 | it('should support re-requiring', () => {
|
77 | assert.equal(mock.reRequire('.'), 'root');
|
78 | });
|
79 |
|
80 | it('should cascade mocks', () => {
|
81 | mock('path', { mocked: true });
|
82 | mock('fs', 'path');
|
83 |
|
84 | const fs = require('fs');
|
85 | assert.equal(fs.mocked, true);
|
86 | });
|
87 |
|
88 | it('should never require the real lib when mocking it', () => {
|
89 | mock('./throw-exception', {});
|
90 | require('./throw-exception');
|
91 | });
|
92 |
|
93 | it('should mock libs required elsewhere', () => {
|
94 | mock('./throw-exception', {});
|
95 | require('./throw-exception-runner');
|
96 | });
|
97 |
|
98 | it('should only load the mocked lib when it is required', () => {
|
99 | mock('./throw-exception', './throw-exception-when-required');
|
100 | try {
|
101 | require('./throw-exception-runner');
|
102 | throw new Error('this line should never be executed.');
|
103 | } catch (error) {
|
104 | assert.equal(error.message, 'this should run when required');
|
105 | }
|
106 | });
|
107 |
|
108 | it('should stop all mocks', () => {
|
109 | mock('fs', {});
|
110 | mock('path', {});
|
111 | const fsMock = require('fs');
|
112 | const pathMock = require('path');
|
113 |
|
114 | mock.stopAll();
|
115 |
|
116 | assert.notEqual(require('fs'), fsMock);
|
117 | assert.notEqual(require('path'), pathMock);
|
118 | });
|
119 |
|
120 | it('should mock a module that does not exist', () => {
|
121 | mock('a', { id: 'a' });
|
122 |
|
123 | assert.equal(require('a').id, 'a');
|
124 | });
|
125 |
|
126 | it('should mock multiple modules that do not exist', () => {
|
127 | mock('a', { id: 'a' });
|
128 | mock('b', { id: 'b' });
|
129 | mock('c', { id: 'c' });
|
130 |
|
131 | assert.equal(require('a').id, 'a');
|
132 | assert.equal(require('b').id, 'b');
|
133 | assert.equal(require('c').id, 'c');
|
134 | });
|
135 |
|
136 | it('should mock a local file that does not exist', () => {
|
137 | mock('./a', { id: 'a' });
|
138 | assert.equal(require('./a').id, 'a');
|
139 |
|
140 | mock('../a', { id: 'a' });
|
141 | assert.equal(require('../a').id, 'a');
|
142 | });
|
143 |
|
144 | it('should mock a local file required elsewhere', () => {
|
145 | mock('./x', { id: 'x' });
|
146 | assert.equal(require('./nested/module-c').dependentOn.id, 'x');
|
147 | });
|
148 |
|
149 | it('should mock multiple local files that do not exist', () => {
|
150 | mock('./a', { id: 'a' });
|
151 | mock('./b', { id: 'b' });
|
152 | mock('./c', { id: 'c' });
|
153 |
|
154 | assert.equal(require('./a').id, 'a');
|
155 | assert.equal(require('./b').id, 'b');
|
156 | assert.equal(require('./c').id, 'c');
|
157 | });
|
158 |
|
159 | it('should unmock a module that is not found', () => {
|
160 | const moduleName = 'module-that-is-not-installed';
|
161 |
|
162 | mock(moduleName, { mocked: true });
|
163 | mock.stop(moduleName);
|
164 |
|
165 | try {
|
166 | require(moduleName);
|
167 | throw new Error('this line should never be executed.');
|
168 | } catch (e) {
|
169 | assert.equal(e.code, 'MODULE_NOT_FOUND');
|
170 | }
|
171 | });
|
172 |
|
173 | it('should differentiate between local files and external modules with the same name', () => {
|
174 | mock('module-a', { id: 'external-module-a' });
|
175 |
|
176 | const b = require('./module-b');
|
177 |
|
178 | assert.equal(b.dependentOn.id, 'local-module-a');
|
179 | assert.equal(b.dependentOn.dependentOn.id, 'external-module-a');
|
180 | });
|
181 |
|
182 | it('should mock files in the node path by the full path', () => {
|
183 | assert.equal(process.env.NODE_PATH, 'test/node-path');
|
184 |
|
185 | mock('in-node-path', { id: 'in-node-path' });
|
186 |
|
187 | const b = require('in-node-path');
|
188 | const c = require('./node-path/in-node-path');
|
189 |
|
190 | assert.equal(b.id, 'in-node-path');
|
191 | assert.equal(c.id, 'in-node-path');
|
192 |
|
193 | assert.equal(b, c);
|
194 | });
|
195 | }); |
\ | No newline at end of file |