forked from mateodelnorte/mockrequire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (60 loc) · 2.1 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
var fs = require('fs'),
log = require('debug')('mockrequire'),
path = require('path'),
stacktrace = require('stack-trace'),
vm = require('vm'),
extend = require('util')._extend;
module.exports = function (module, mocks, compiler) {
// allow specifying modules with shorthand when named index.js
if (module.slice(-1) === '/') { module = module + 'index' }
mocks = mocks || {};
// 3rd object in stacktrace array is calling file
var caller = stacktrace.get(this)[2].getFileName();
log('preparing to mockrequire ' + module + ' from module ' + caller);
var isNodeModule = function(module) {
return module[0] !== '.';
};
var resolve = function (module, base) {
if (isNodeModule(module)) return module;
return path.resolve(path.dirname(base), module) + '.js';
};
var findNodeModule = function(file, name) {
var modulesFolder = path.join(file, '../node_modules');
if(fs.existsSync(modulesFolder)){
return path.join(modulesFolder, name);
} else {
file = path.join(file, '../');
return findNodeModule(file, name);
}
};
var exports = {};
var sandbox = {
require: function (name) {
if (mocks[name]) {
log('loading mocked module ' + name + ' from parent module ' + module);
return mocks[name];
} else if (isNodeModule(name)) {
log('loading node_module ' + file + ' from parent module ' + module);
return require(name);
} else {
var file = path.join(path.dirname(caller), path.dirname(module), name);
//check if its a node module if so find module in node_modules dir
if(name.charAt(0) !== '.') {
file = findNodeModule(file, name);
}
log('loading module ' + file + ' from parent module ' + module);
return require(file);
}
},
exports: exports,
module: {
exports: exports
}
};
extend(sandbox, global);
var filepath = resolve(module, caller);
log('mockrequiring ' + filepath);
compiler = compiler || fs.readFileSync;
vm.runInNewContext(compiler(filepath), sandbox);
return sandbox.module.exports;
};