UNPKG

rewiremock

Version:

Advanced dependency mocking device.

52 lines (46 loc) 1.46 kB
import { getAllMocks } from './mocks'; import { shouldWipe } from './plugins'; import { relativeWipeCheck } from "./plugins/relative"; import Module from './module'; // which one? export var wipe = typeof __webpack_require__ === 'function' ? require('wipe-webpack-cache') : require('wipe-node-cache').wipeCache; var primaryResolver = function primaryResolver(stubs, moduleName) { return stubs[moduleName]; }; var resolver = function resolver(stubs, moduleName) { // never wipe .node(native) module if (moduleName.indexOf('.node') > -1) { return false; } return 0 || shouldWipe(stubs, moduleName) || primaryResolver(stubs, moduleName) || relativeWipeCheck(stubs, moduleName); }; var wipeCache = function wipeCache(primaryCache) { if (primaryCache) { // post clean wipe(primaryCache, primaryResolver); } else { // pre clean wipe(getAllMocks(), resolver); } }; export function safelyRemoveCache(moduleName) { var m = Module._cache[moduleName]; if (m) { // remove self from own parents if (m.parent && m.parent.children) { m.parent.children = m.parent.children.filter(function (x) { return x !== m; }); } // remove self from own children if (m.children) { m.children.forEach(function (child) { if (child.parent && child.parent === m) { child.parent = null; } }); } delete Module._cache[moduleName]; } } export default wipeCache;