forked from aca-mobile/ti-unit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockrequire.js
38 lines (28 loc) · 935 Bytes
/
mockrequire.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
function MockRequire() {
(function (module) {
var rL = module._load;
module._load = function (request, parent, isMain) {
if(_hasMock(request)) {
console.info('\n[INFO] Require is returning a mock implementation for: ' + request);
return _getMock(request);
}
// we always need to bypass/reset the cache - if not, some strange errors can occur during the execution of a test suite
module._cache = [];
return rL(request, parent, isMain);
};
}(require('module')));
var _mocks = [];
this.addMock = function(path, mock){
_mocks[path] = mock;
};
function _hasMock(path){
return _mocks[path] != undefined;
};
function _getMock(path){
return _mocks[path];
};
this.resetMocks = function(){
_mocks = [];
}
}
module.exports = new MockRequire();