forked from thlorenz/proxyquireify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (81 loc) · 3.03 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
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
var keys = require('amp-keys');
var each = require('amp-each');
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;
}
function fillMissingKeys(mdl, original) {
each(keys(original), function (key) {
if (!mdl[key]) mdl[key] = original[key];
});
if (typeof mdl === 'function' && typeof original === 'function') {
each(keys(original.prototype), function (key) {
if (!mdl.prototype[key]) mdl.prototype[key] = original.prototype[key];
});
}
return mdl;
}
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) return original();
var stub = stubs[request];
if (!stub) return original();
var stubWideNoCallThru = !!stubs['@noCallThru'] && stub['@noCallThru'] !== false;
var noCallThru = stubWideNoCallThru || !!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;
}