-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
93 lines (70 loc) · 2.82 KB
/
index.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
'use strict';
var fillMissingKeys = require('fill-keys');
var moduleNotFoundError = require('module-not-found-error');
function ProxyquireifyError(msg) {
this.name = 'ProxyquireifyError';
Error.captureStackTrace(this, ProxyquireifyError);
this.message = msg || 'An error occurred inside proxyquireify.';
}
function validateArguments(request, stubs) {
var msg = (function getMessage() {
if (!request)
return 'Missing argument: "request". Need it to resolve desired module.';
if (!stubs)
return 'Missing argument: "stubs". If no stubbing is needed, use regular require instead.';
if (typeof request != 'string')
return 'Invalid argument: "request". Needs to be a requirable string that is the module to load.';
if (typeof stubs != 'object')
return 'Invalid argument: "stubs". Needs to be an object containing overrides e.g., {"path": { extname: function () { ... } } }.';
})();
if (msg) throw new ProxyquireifyError(msg);
}
var stubs;
function stub(stubs_) {
stubs = stubs_;
// This cache is used by the prelude as an alternative to the regular cache.
// It is not read or written here, except to set it to an empty object when
// adding stubs and to reset it to null when clearing stubs.
module.exports._cache = {};
}
function reset() {
stubs = undefined;
module.exports._cache = null;
}
var proxyquire = module.exports = function (require_) {
if (typeof require_ != 'function')
throw new ProxyquireifyError(
'It seems like you didn\'t initialize proxyquireify with the require in your test.\n'
+ 'Make sure to correct this, i.e.: "var proxyquire = require(\'proxyquireify\')(require);"'
);
reset();
return function(request, stubs) {
validateArguments(request, stubs);
// set the stubs and require dependency
// when stub require is invoked by the module under test it will find the stubs here
stub(stubs);
var dep = require_(request);
reset();
return dep;
};
};
// Start with the default cache
proxyquire._cache = null;
proxyquire._proxy = function (require_, request) {
function original() {
return require_(request);
}
if (!stubs || !stubs.hasOwnProperty(request)) return original();
var stub = stubs[request];
if (stub === null) throw moduleNotFoundError(request)
var stubWideNoCallThru = Boolean(stubs['@noCallThru']) && (stub == null || stub['@noCallThru'] !== false);
var noCallThru = stubWideNoCallThru || (stub != null && Boolean(stub['@noCallThru']));
return noCallThru ? stub : fillMissingKeys(stub, original());
};
if (require.cache) {
// only used during build, so prevent browserify from including it
var replacePreludePath = './lib/replace-prelude';
var replacePrelude = require(replacePreludePath);
proxyquire.browserify = replacePrelude.browserify;
proxyquire.plugin = replacePrelude.plugin;
}