mock-require-lazy
Version:
Simple, intuitive mocking of Node.js modules. Fork of mock-require adding lazy require and is a drop in replacement for mock-require
101 lines • 4.02 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _getcallerfile = /*#__PURE__*/ _interop_require_default(require("get-caller-file"));
var _module = /*#__PURE__*/ _interop_require_default(require("module"));
var _normalizepath = /*#__PURE__*/ _interop_require_default(require("normalize-path"));
var _path = require("path");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
var Module = _module.default;
var mockExports = {};
var pendingMockExports = {};
// biome-ignore lint/suspicious/noShadowRestrictedNames: Legacy
var hasOwnProperty = {}.hasOwnProperty;
var originalLoader = Module._load;
Module._load = function(request, parent) {
// biome-ignore lint/complexity/noArguments: Apply arguments
if (!parent) return originalLoader.apply(this, arguments);
var fullFilePath = getFullPathNormalized(request, parent.filename);
if (hasOwnProperty.call(pendingMockExports, fullFilePath)) {
var pending = pendingMockExports[fullFilePath];
var mockExport = pending.lazy ? pending.mockExport() : pending.mockExport;
mockExports[fullFilePath] = typeof mockExport === 'string' ? require(getFullPathNormalized(mockExport, pending.calledFrom)) : mockExport;
delete pendingMockExports[fullFilePath];
}
// biome-ignore lint/complexity/noArguments: Apply arguments
return hasOwnProperty.call(mockExports, fullFilePath) ? mockExports[fullFilePath] : originalLoader.apply(this, arguments);
};
function startMocking(path, mockExport, lazy) {
var calledFrom = (0, _getcallerfile.default)();
pendingMockExports[getFullPathNormalized(path, calledFrom)] = {
mockExport: mockExport,
calledFrom: calledFrom,
lazy: lazy
};
}
function stopMocking(path) {
var calledFrom = (0, _getcallerfile.default)();
var fullPath = getFullPathNormalized(path, calledFrom);
delete pendingMockExports[fullPath];
delete mockExports[fullPath];
}
function stopMockingAll() {
mockExports = {};
pendingMockExports = {};
}
function reRequire(path) {
var _$module = getFullPathNormalized(path, (0, _getcallerfile.default)());
delete require.cache[require.resolve(_$module)];
return require(_$module);
}
function isInNodePath(resolvedPath) {
if (!resolvedPath) return false;
return Module.globalPaths.map(function(nodePath) {
return (0, _path.resolve)(process.cwd(), nodePath) + _path.sep;
}).some(function(fullNodePath) {
return resolvedPath.indexOf(fullNodePath) === 0;
});
}
function getFullPath(path, calledFrom) {
var resolvedPath;
try {
resolvedPath = require.resolve(path);
} catch (_e) {
// do nothing
}
var isLocalModule = /^\.{1,2}[/\\]?/.test(path);
var isInPath = isInNodePath(resolvedPath);
var isExternal = !isLocalModule && /[/\\]node_modules[/\\]/.test(resolvedPath);
var isSystemModule = resolvedPath === path;
if (isExternal || isSystemModule || isInPath) {
return resolvedPath;
}
if (!isLocalModule) {
return path;
}
var localModuleName = (0, _path.join)((0, _path.dirname)(calledFrom), path);
try {
return Module._resolveFilename(localModuleName);
} catch (e) {
if (isModuleNotFoundError(e)) {
return localModuleName;
}
throw e;
}
}
function getFullPathNormalized(path, calledFrom) {
return (0, _normalizepath.default)(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;
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
;