-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (106 loc) · 3.6 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
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
'use strict';
var fillMissingKeys = require( 'fill-keys' );
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;
var noCallThruGlobal = false;
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();
var prxCall = 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;
};
prxCall.noCallThru = function () {
noCallThruGlobal = true;
return prxCall;
};
prxCall.callThru = function () {
noCallThruGlobal = false;
return prxCall;
};
prxCall.reset = reset;
return prxCall;
};
var clsc = function () {
var args = arguments;
args = [ ].slice.call( args );
for (var i = 0, len = args.length; i < len; i++) {
var current = args[ i ];
if ( typeof current !== 'undefined' && current !== null ) {
return current;
}
}
return null;
};
// 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();
if ( typeof stub === 'undefined' ) {
return original();
}
// this allows to replace the modules with null to simulate
// they cannot be loaded
if ( stub === null ) {
return null;
}
var noCallThru = false;
if ( !noCallThruGlobal ) {
var stubWideNoCallThru = !!stubs[ '@noCallThru' ] && stub[ '@noCallThru' ] !== false;
noCallThru = stubWideNoCallThru || !!stub[ '@noCallThru' ];
} else {
noCallThru = clsc( stub[ '@noCallThru' ], stubs[ '@noCallThru' ], noCallThruGlobal )
}
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;
}