Ghost/ghost/core/test/utils/mocks/modules.js
Daniel Lockyer 3d989eba23 Converted Ghost repo into a monorepo
refs https://github.com/TryGhost/Toolbox/issues/354

- this commit turns the Ghost repo into a monorepo so we can bring our
  internal packages back in, which makes life easier when working on
  Ghost
2022-07-20 16:41:05 +02:00

24 lines
631 B
JavaScript

const Module = require('module');
const originalRequireFn = Module.prototype.require;
/**
* helper fn to mock non-existent modules
* mocks.modules.mockNonExistentModule(/pattern/, mockedModule)
*/
exports.mockNonExistentModule = (modulePath, module, error = false) => {
Module.prototype.require = function (path) {
if (path.match(modulePath)) {
if (error) {
throw module;
}
return module;
}
return originalRequireFn.apply(this, arguments);
};
};
exports.unmockNonExistentModule = () => {
Module.prototype.require = originalRequireFn;
};