forked from mfncooper/mockery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockery.js
236 lines (214 loc) · 7.9 KB
/
mockery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyrights for code authored by Yahoo! Inc. is licensed under the following
terms:
MIT License
Copyright (c) 2011 Yahoo! Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/*
* A library that enables the hooking of the standard 'require' function, such
* that a (possibly partial) mock implementation can be provided instead. This
* is most useful for running unit tests, since any dependency obtained through
* 'require' can be mocked out.
*/
var m = require('module'),
registeredMocks = {},
registeredSubstitutes = {},
registeredAllowables = {},
originalLoader = null,
warnIfUnregistered = true;
/*
* The (private) loader replacement that is used when hooking is enabled. It
* does the work of returning a mock or substitute when configured, reporting
* non-allowed modules, and invoking the original loader when appropriate.
* The signature of this function *must* match that of Node's Module._load,
* since it will replace that when mockery is enabled.
*/
function hookedLoader(request, parent, isMain) {
var subst, allow, file;
if (!originalLoader) {
throw new Error("Loader has not been hooked");
}
if (registeredMocks.hasOwnProperty(request)) {
return registeredMocks[request];
} else if (registeredSubstitutes.hasOwnProperty(request)) {
subst = registeredSubstitutes[request];
if (!subst.module && subst.name) {
subst.module = originalLoader(subst.name, parent, isMain);
}
if (!subst.module) {
throw new Error("Misconfigured substitute for '" + request + "'");
}
return subst.module;
} else {
if (registeredAllowables.hasOwnProperty(request)) {
allow = registeredAllowables[request];
if (allow.unhook) {
file = m._resolveFilename(request, parent)[1];
if (file.indexOf('/') !== -1 && allow.paths.indexOf(file) === -1) {
allow.paths.push(file);
}
}
} else {
if (warnIfUnregistered) {
console.warn("WARNING: loading non-allowed module: " + request);
}
}
return originalLoader(request, parent, isMain);
}
}
/*
* Enables mockery by hooking subsequent 'require' invocations. Note that *all*
* 'require' invocations will be hooked until 'disable' is called. Calling this
* function more than once will have no ill effects.
*/
function enable() {
if (originalLoader) {
// Already hooked
return;
}
/*jslint nomen: true */
originalLoader = m._load;
m._load = hookedLoader;
}
/*
* Disables mockery by unhooking from the Node loader. No subsequent 'require'
* invocations will be seen by mockery. Calling this function more than once
* will have no ill effects.
*/
function disable() {
if (!originalLoader) {
// Not hooked
return;
}
/*jslint nomen: true */
m._load = originalLoader;
originalLoader = null;
}
/*
* Enable or disable warnings to the console when modules are loaded that have
* not been registered as a mock, a substitute, or allowed.
*/
function warnOnUnregistered(enable) {
warnIfUnregistered = enable;
}
/*
* Register a mock object for the specified module. While mockery is enabled,
* any subsequent 'require' for this module will return the mock object. The
* mock need not mock out all original exports, but no fallback is provided
* for anything not mocked and subsequently invoked.
*/
function registerMock(mod, mock) {
if (registeredMocks.hasOwnProperty(mod)) {
console.warn("WARNING: Replacing existing mock for module: " + mod);
}
registeredMocks[mod] = mock;
}
/*
* Deregister a mock object for the specified module. A subsequent 'require' for
* that module will revert to the previous behaviour (which, by default, means
* falling back to the original 'require' behaviour).
*/
function deregisterMock(mod) {
if (registeredMocks.hasOwnProperty(mod)) {
delete registeredMocks[mod];
}
}
/*
* Register a substitute module for the specified module. While mockery is
* enabled, any subsequent 'require' for this module will be effectively
* replaced by a 'require' for the substitute module. This is useful when
* a mock implementation is itself implemented as a module.
*/
function registerSubstitute(mod, subst) {
if (registeredSubstitutes.hasOwnProperty(mod)) {
console.warn("WARNING: Replacing existing substitute for module: " + mod);
}
registeredSubstitutes[mod] = {
name: subst
};
}
/*
* Deregister a substitute module for the specified module. A subsequent
* 'require' for that module will revert to the previous behaviour (which, by
* default, means falling back to the original 'require' behaviour).
*/
function deregisterSubstitute(mod) {
if (registeredSubstitutes.hasOwnProperty(mod)) {
delete registeredSubstitutes[mod];
}
}
/*
* Register a module as 'allowed', meaning that, even if a mock or substitute
* for it has not been registered, mockery will not complain when it is loaded
* via 'require'. This encourages the user to consciously declare the modules
* that will be loaded and used in the original form, thus avoiding warnings.
*
* If 'unhook' is true, the module will be removed from the module cache when
* it is deregistered.
*/
function registerAllowable(mod, unhook) {
registeredAllowables[mod] = {
unhook: !!unhook,
paths: []
};
}
/*
* Deregister a module as 'allowed'. A subsequent 'require' for that module
* will generate a warning that the module is not allowed, unless or until a
* mock or substitute is registered for that module.
*/
function deregisterAllowable(mod) {
if (registeredAllowables.hasOwnProperty(mod)) {
var allow = registeredAllowables[mod];
if (allow.unhook) {
allow.paths.forEach(function (p) {
delete m._cache[p];
});
}
delete registeredAllowables[mod];
}
}
/*
* Deregister all mocks, substitutes, and allowed modules, resetting the state
* to a clean slate. This does not affect the enabled / disabled state of
* mockery, though.
*/
function deregisterAll() {
Object.keys(registeredAllowables).forEach(function (mod) {
var allow = registeredAllowables[mod];
if (allow.unhook) {
allow.paths.forEach(function (p) {
delete m._cache[p];
});
}
});
registeredMocks = {};
registeredSubstitutes = {};
registeredAllowables = {};
}
// Exported functions
exports.enable = enable;
exports.disable = disable;
exports.warnOnUnregistered = warnOnUnregistered;
exports.registerMock = registerMock;
exports.registerSubstitute = registerSubstitute;
exports.registerAllowable = registerAllowable;
exports.deregisterMock = deregisterMock;
exports.deregisterSubstitute = deregisterSubstitute;
exports.deregisterAllowable = deregisterAllowable;
exports.deregisterAll = deregisterAll;