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
131 lines • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: Object.getOwnPropertyDescriptor(all, name).get
});
}
_export(exports, {
get default () {
return mock;
},
get reRequire () {
return reRequire;
},
get stop () {
return stop;
},
get stopAll () {
return stopAll;
}
});
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");
var _url = /*#__PURE__*/ _interop_require_default(require("url"));
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
var Module = _module.default;
var _require = typeof require === 'undefined' ? _module.default.createRequire(require("url").pathToFileURL(__filename).toString()) : require;
var mockExports = {};
var pendingMockExports = {};
// biome-ignore lint/suspicious/noShadowRestrictedNames: Legacy
var hasOwnProperty = {}.hasOwnProperty;
var startsWith = function startsWith(string, check) {
return string.lastIndexOf(check, 0) === 0;
};
var originalLoader = Module._load;
Module._load = function(request, parent) {
// biome-ignore lint/complexity/noArguments: Apply arguments
// biome-ignore lint/complexity/noBannedTypes: .apply bypass needed for IArguments
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
// biome-ignore lint/complexity/noBannedTypes: .apply bypass needed for IArguments
return hasOwnProperty.call(mockExports, fullFilePath) ? mockExports[fullFilePath] : originalLoader.apply(this, arguments);
};
function stripFileProtocol(calledFrom) {
return calledFrom.indexOf('file://', 0) === 0 ? _url.default.fileURLToPath(calledFrom) : calledFrom;
}
function mock(path, mockExport, lazy) {
var calledFrom = stripFileProtocol((0, _getcallerfile.default)());
pendingMockExports[getFullPathNormalized(path, calledFrom)] = {
mockExport: mockExport,
calledFrom: calledFrom,
lazy: lazy
};
}
var stop = function stopMocking(path) {
var calledFrom = stripFileProtocol((0, _getcallerfile.default)());
var fullPath = getFullPathNormalized(path, calledFrom);
delete pendingMockExports[fullPath];
delete mockExports[fullPath];
};
var stopAll = function stopMockingAll() {
mockExports = {};
pendingMockExports = {};
};
var reRequire = function reRequire(path) {
var module = getFullPathNormalized(path, stripFileProtocol((0, _getcallerfile.default)()));
delete _require.cache[_require.resolve(module)];
return _require(module);
};
mock.stop = stop;
mock.stopAll = stopAll;
mock.reRequire = reRequire;
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 startsWith(resolvedPath, fullNodePath);
});
}
function getFullPath(path, calledFrom) {
var resolvedPath = null;
try {
resolvedPath = require.resolve(path);
} catch (_e) {
// do nothing
}
var isLocalModule = /^\.{1,2}[/\\]?/.test(path);
var isInPath = isInNodePath(resolvedPath);
var isExternal = !isLocalModule && resolvedPath !== null && /[/\\]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) {
var fullPath = getFullPath(path, calledFrom);
return (0, _normalizepath.default)(fullPath !== null && fullPath !== void 0 ? fullPath : path);
}
function isModuleNotFoundError(e) {
return e.code && e.code === 'MODULE_NOT_FOUND';
}
/* 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; }