Ghost/ghost/image-transform/test/utils/modules.js
Hannah Wolfe 80e320fd83 Cleanup repeated module mocking utils
- mock non existant module util was defined twice
- split it out properly from the rest of the utils, update all references
- this allows us to move this util out of the codebase along with other code, e.g. the image manipulation code
2020-03-25 14:48:41 +00: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;
};