This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
113 lines (90 loc) · 2.98 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
'use strict';
const Module = require('module');
const dirname = require('path').dirname;
const join = require('path').join;
const resolve = require('path').resolve;
const pathsep = require('path').sep;
const getCallerFile = require('get-caller-file');
const normalize = require('normalize-path');
const originalLoader = Module._load;
let mockExports = {};
let pendingMockExports = {};
Module._load = function(request, parent) {
if (!parent) return originalLoader.apply(this, arguments);
const fullFilePath = getFullPathNormalized(request, parent.filename);
if (pendingMockExports.hasOwnProperty(fullFilePath)) {
mockExports[fullFilePath] = typeof pendingMockExports[fullFilePath] === 'string' ?
require(pendingMockExports[fullFilePath]) :
pendingMockExports[fullFilePath];
delete pendingMockExports[fullFilePath];
}
return mockExports.hasOwnProperty(fullFilePath)
? mockExports[fullFilePath]
: originalLoader.apply(this, arguments);
};
function startMocking(path, mockExport) {
const calledFrom = getCallerFile();
if (typeof mockExport === 'string') {
mockExport = getFullPathNormalized(mockExport, calledFrom);
}
pendingMockExports[getFullPathNormalized(path, calledFrom)] = mockExport;
}
function stopMocking(path) {
const calledFrom = getCallerFile();
const fullPath = getFullPathNormalized(path, calledFrom);
delete pendingMockExports[fullPath];
delete mockExports[fullPath];
}
function stopMockingAll() {
mockExports = {};
pendingMockExports = {};
}
function reRequire(path) {
const module = getFullPathNormalized(path, getCallerFile());
delete require.cache[require.resolve(module)];
return require(module);
}
function isInNodePath(resolvedPath) {
if (!resolvedPath) return false;
return Module.globalPaths
.map((nodePath) => {
return resolve(process.cwd(), nodePath) + pathsep;
})
.some((fullNodePath) => {
return resolvedPath.indexOf(fullNodePath) === 0;
});
}
function getFullPath(path, calledFrom) {
let resolvedPath;
try {
resolvedPath = require.resolve(path);
} catch (e) {
// do nothing
}
const isLocalModule = /^\.{1,2}[/\\]?/.test(path);
const isInPath = isInNodePath(resolvedPath);
const isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath);
const isSystemModule = resolvedPath === path;
if (isExternal || isSystemModule || isInPath) {
return resolvedPath;
}
if (!isLocalModule) {
return path;
}
const localModuleName = join(dirname(calledFrom), path);
try {
return Module._resolveFilename(localModuleName);
} catch (e) {
if (isModuleNotFoundError(e)) { return localModuleName; } else { throw e; }
}
}
function getFullPathNormalized(path, calledFrom) {
return normalize(getFullPath(path, calledFrom));
}
function isModuleNotFoundError(e) {
return e.code && e.code === 'MODULE_NOT_FOUND';
}
module.exports = startMocking;
module.exports.stop = stopMocking;
module.exports.stopAll = stopMockingAll;
module.exports.reRequire = reRequire;