From d9f164c56730184b8642cb0299b172040b5c57c5 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Sun, 20 Oct 2019 10:50:26 +1100 Subject: [PATCH 1/7] feat: new cache management --- _tests/cache.spec.js | 46 +++++++++++++++++++++++++++++++++++- _tests/lib/cache/requireA.js | 2 ++ _tests/mock.spec.js | 12 +++++++++- _tests/scope.spec.js | 3 ++- package.json | 2 +- src/executor.js | 1 + src/mockModule.js | 2 +- src/module.js | 7 ++++++ src/plugins/directChild.js | 4 ++-- yarn.lock | 8 +++---- 10 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 _tests/lib/cache/requireA.js diff --git a/_tests/cache.spec.js b/_tests/cache.spec.js index e9646d9..34011a1 100644 --- a/_tests/cache.spec.js +++ b/_tests/cache.spec.js @@ -1,7 +1,9 @@ import {expect} from 'chai'; import path from 'path' + +import Module from '../src/module'; import rewiremock from '../src/index'; -import { wipe } from '../src/wipeCache'; +import {wipe} from '../src/wipeCache'; describe('cache', () => { @@ -10,6 +12,48 @@ describe('cache', () => { wipe(path.dirname(__filename), wipeAll); }; + it('should handle cache properly', () => { + const test1 = rewiremock.proxy('./lib/cache/requireA', r => { + rewiremock(() => require('./lib/cache/a')).with(42).toBeUsed(); + }); + expect(test1.b).to.be.equal(42); + + const fileA = Module._resolveFilename('./lib/cache/a', module); + expect(require.cache[fileA]).to.be.equal(undefined); + + let test2; + + expect(() => { + test2 = rewiremock.proxy('./lib/cache/requireA', r => { + rewiremock(() => require('./lib/cache/a')).with(42).toBeUsed(); + }) + }).not.to.throw(); + + expect(require.cache[fileA]).to.be.equal(undefined); + + expect(test2.b).to.be.equal(42); + expect(test1.a).not.to.be.equal(test2.a); + }); + + it('should restore cache properly', () => { + const fileA = Module._resolveFilename('./lib/cache/a', module); + + const test0 = require('./lib/cache/requireA').a; + + const cache0 =require.cache[fileA]; + expect(cache0).not.to.be.equal(undefined); + + const test1 = rewiremock.proxy('./lib/cache/requireA', r => { + rewiremock(() => require('./lib/cache/a')).with(42).toBeUsed(); + }); + expect(test1.a).not.to.be.equal(test0); + expect(require.cache[fileA]).to.be.equal(cache0); + + const test2 = require('./lib/cache/requireA').a; + + expect(test2).to.be.equal(test0); + }); + it('should clear mocked modules cache', () => { rewiremock.inScope(() => { resetCache(); diff --git a/_tests/lib/cache/requireA.js b/_tests/lib/cache/requireA.js new file mode 100644 index 0000000..17a5709 --- /dev/null +++ b/_tests/lib/cache/requireA.js @@ -0,0 +1,2 @@ +module.exports.a = {}; +module.exports.b = require('./a'); \ No newline at end of file diff --git a/_tests/mock.spec.js b/_tests/mock.spec.js index 3f979d9..267bd1c 100644 --- a/_tests/mock.spec.js +++ b/_tests/mock.spec.js @@ -259,10 +259,20 @@ describe('rewiremock ', () => { expect(mocked()).to.be.equal('mocked|notmocked-b'); }); + it('should mock only direct child : ', () => { + return rewiremock.around(() => require('./lib/c/barbaz.js'), + () => { + rewiremock(() => import('./lib/c/baz')) + .with(() => 'mock') + .directChildOnly() + .calledFromMock(); + }) + .then(mocked => expect(mocked()).to.be.equal('>+!mock')); + }); + it('should mock only direct child due to callThrough mocked / async : ', () => { return rewiremock.around(() => require('./lib/c/barbaz.js'), () => { - //addPlugin(nodePlugin); rewiremock(() => import('./lib/c/bar')) .callThrough(); rewiremock(() => import('./lib/c/baz')) diff --git a/_tests/scope.spec.js b/_tests/scope.spec.js index d3e1eab..65ee2db 100644 --- a/_tests/scope.spec.js +++ b/_tests/scope.spec.js @@ -8,7 +8,7 @@ import nodePlugin from '../src/plugins/nodejs'; describe('scope ', () => { const webpackDefault = exports => { - return typeof __webpack_require__ !== 'undefined' ? exports.default: exports; + return exports.default || exports; }; it('scope test: ', () => { @@ -62,6 +62,7 @@ describe('scope ', () => { mock('./lib/a/../b/baz').with(() => 'cc'); }) .then((mockedBaz) => { + console.log(webpackDefault, webpackDefault(mockedBaz)); expect(webpackDefault(mockedBaz)()).to.be.equal('aabbcc'); }); }); diff --git a/package.json b/package.json index 4295d44..f366dee 100644 --- a/package.json +++ b/package.json @@ -103,7 +103,7 @@ "conventional-changelog-cli": "^2.0.21", "eslint": "^4.19.1", "eslint-plugin-mocha": "^5.0.0", - "esm": "^3.2.22", + "esm": "^3.2.25", "flow-bin": "^0.75.0", "jest-cli": "^23.0.0", "karma": "^2.0.2", diff --git a/src/executor.js b/src/executor.js index 9f3d0c9..cbce109 100644 --- a/src/executor.js +++ b/src/executor.js @@ -271,6 +271,7 @@ function mockLoader(request, parent, isMain) { return mockResult(request, mock, () => mock.value); } else { + // console.error('reject', baseRequest, parent.id, parent.parent.id); mock.rejected = mock.rejected || []; if (shouldResult.plugins) { mock.rejected.push({ diff --git a/src/mockModule.js b/src/mockModule.js index d7f4891..edf8760 100644 --- a/src/mockModule.js +++ b/src/mockModule.js @@ -249,7 +249,7 @@ const restoreCache = () => { if(oldCache) { Object .keys(oldCache) - .filter(key => !newCache[key]) + //.filter(key => !newCache[key]) .forEach(key => (newCache[key] = oldCache[key])); mockScope.requireCache = null; diff --git a/src/module.js b/src/module.js index a88a0ea..74e7bf3 100644 --- a/src/module.js +++ b/src/module.js @@ -79,5 +79,12 @@ export const inParents = (a, b) => { return parents.some(x => x === B); } +export const isParent = (a, b) => { + const B = getModuleName(b) + const parents = getModuleParents(a); + return parents[0] === B; +} + + export default NodeModule; diff --git a/src/plugins/directChild.js b/src/plugins/directChild.js index 7049c38..083ba5a 100644 --- a/src/plugins/directChild.js +++ b/src/plugins/directChild.js @@ -1,11 +1,11 @@ import createPlugin, {PASS, NO} from './_common'; import getScope from '../globals'; -import {inParents, getModuleName} from '../module'; +import {isParent, getModuleName} from '../module'; const shouldMock = (mock, request, parent, topModule) => { if (mock.flag_directChildOnly) { - return inParents(parent, topModule) ? PASS : NO; + return isParent(parent, topModule) ? PASS : NO; } if (mock.flag_toBeCalledFromMock) { const {mockedModules} = getScope(); diff --git a/yarn.lock b/yarn.lock index 6c7b87a..38f1e50 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3116,10 +3116,10 @@ eslint@^4.19.1: table "4.0.2" text-table "~0.2.0" -esm@^3.2.22: - version "3.2.23" - resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.23.tgz#caa54cb844a5ea1b89f9aa6c39ef7afb940a78fd" - integrity sha512-p7iNpE0K3nLn1KE2O0Vz/2Gpg93U+JroVqAdHZwK7l3MmPKh4iu5CEvw1Gym9DT23BgNNvnY5wOf9vMjBFw7Ug== +esm@^3.2.25: + version "3.2.25" + resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10" + integrity sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA== espree@^3.5.4: version "3.5.4" From 98e559c312af4dfef0f5326a144dddb4ef68b227 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Fri, 1 May 2020 19:41:19 +1000 Subject: [PATCH 2/7] clear rewiremock reference from a parent module --- src/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 8e07d4a..459a3ce 100644 --- a/src/index.js +++ b/src/index.js @@ -14,8 +14,20 @@ if (!getModuleParent(module)) { throw new Error('Rewiremock: there is no "parent module". Is there two HotModuleReplacementPlugins?'); } -delete require.cache[path.join(path.dirname(__filename), './mockModule.js')]; -delete require.cache[moduleName.replace('index.js', 'mockModule.js')]; +function safelyRemoveCache(moduleName) { + const m = require.cache[moduleName]; + if(m) { + if(m.parent && m.parent.children){ + m.parent.children = m.parent.children.filter(x => x!==m); + } + delete require.cache[moduleName] + } +} + +// delete core +safelyRemoveCache(path.join(path.dirname(__filename), './mockModule.js')); +// delete self +safelyRemoveCache(moduleName.replace('index.js', 'mockModule.js')); import * as API from './mockModule'; import applyDefaultConfig from "./plugins/defaultConfig"; @@ -26,7 +38,7 @@ export const cleanup = () => { }; export const overrideEntryPoint = (module) => { - delete require.cache[getModuleName(module)]; + safelyRemoveCache(getModuleName(module)); API.mockModule.overrideEntryPoint(getModuleParent(module)); //API.cleanup(); }; From 88ca28e042d68ae6877f6d7ac016c2a014d1d140 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Fri, 1 May 2020 19:41:38 +1000 Subject: [PATCH 3/7] cleanup useless methods --- src/plugins/relative.js | 2 -- src/plugins/webpack-alias.js | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/plugins/relative.js b/src/plugins/relative.js index 86cf1fe..26ac5e9 100644 --- a/src/plugins/relative.js +++ b/src/plugins/relative.js @@ -17,7 +17,6 @@ export const relativeWipeCheck = (stubs, moduleName) => { }; const fileNameTransformer = (fileName/*, module*/) => fileName; -//const wipeCheck = (stubs, moduleName) => relativeWipeCheck(stubs, moduleName); const shouldMock = (mock, request, parent, topModule) => { if(mock.flag_directChildOnly === false) { @@ -28,7 +27,6 @@ const shouldMock = (mock, request, parent, topModule) => { const plugin = createPlugin({ fileNameTransformer, - //wipeCheck, shouldMock, name: 'relative' diff --git a/src/plugins/webpack-alias.js b/src/plugins/webpack-alias.js index 7d2cb02..deb8cdc 100644 --- a/src/plugins/webpack-alias.js +++ b/src/plugins/webpack-alias.js @@ -14,15 +14,12 @@ const fileNameTransformer = (fileName) => { return processFile(fileName, settings); }; -const wipeCheck = (stubs, moduleName) => standardWipeCheck(stubs, moduleName); - export { configure } export default createPlugin({ fileNameTransformer, - wipeCheck, name: 'webpack-alias' }); From 49ac22ae6fa8406ee63e5499972e3bfc2eb60df1 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Sat, 2 May 2020 19:53:02 +1000 Subject: [PATCH 4/7] fix: update wipe-node-cache to prevent memory leak in the cache management --- package.json | 2 +- yarn.lock | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8c10158..e794301 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "lodash.template": "^4.4.0", "node-libs-browser": "^2.1.0", "path-parse": "^1.0.5", - "wipe-node-cache": "^2.1.0", + "wipe-node-cache": "^2.1.2", "wipe-webpack-cache": "^2.1.0" }, "devDependencies": { diff --git a/yarn.lock b/yarn.lock index 38f1e50..aa23c7a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9125,6 +9125,11 @@ wipe-node-cache@^2.1.0: resolved "https://registry.yarnpkg.com/wipe-node-cache/-/wipe-node-cache-2.1.0.tgz#9dc20a2f881c1e5fe3f4a6e8a98047079d97acf6" integrity sha512-Vdash0WV9Di/GeYW9FJrAZcPjGK4dO7M/Be/sJybguEgcM7As0uwLyvewZYqdlepoh7Rj4ZJKEdo8uX83PeNIw== +wipe-node-cache@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/wipe-node-cache/-/wipe-node-cache-2.1.2.tgz#f5aef4bc4366866f89536f3352eb6b9deda53ca0" + integrity sha512-m7NXa8qSxBGMtdQilOu53ctMaIBXy93FOP04EC1Uf4bpsE+r+adfLKwIMIvGbABsznaSNxK/ErD4xXDyY5og9w== + wipe-webpack-cache@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/wipe-webpack-cache/-/wipe-webpack-cache-2.1.0.tgz#bc26149f21cf1a3c34752997b96458b567d3e6a1" From dd9df1d278fea2f5eeae658d604b152625036e0b Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Thu, 7 May 2020 16:17:35 +1000 Subject: [PATCH 5/7] fix: update cache controls --- rewiremock.d.ts | 7 +- src/mockModule.js | 258 ++++++++++++++++++++++++---------------------- src/wipeCache.js | 11 +- 3 files changed, 146 insertions(+), 130 deletions(-) diff --git a/rewiremock.d.ts b/rewiremock.d.ts index 439b3d5..604a218 100644 --- a/rewiremock.d.ts +++ b/rewiremock.d.ts @@ -256,8 +256,13 @@ declare module 'rewiremock' { /** * set aggressive politics to cache operation, restoring to the the previous values on end. + * false: (default) removes all new elements from the cache. Old data from "old" cache is transferred to a new one. New modules are kept. + * true: removes mocked modules from the cache. New modules are kept + * 'nocache': completely restores old modules */ - forceCacheClear(): rewiremock; + forceCacheClear(mode?: boolean | 'nocache'): rewiremock; + + setCacheControl(enable: boolean): rewiremock; /** * Adding new isolationpassby record diff --git a/src/mockModule.js b/src/mockModule.js index edf8760..3cc32ca 100644 --- a/src/mockModule.js +++ b/src/mockModule.js @@ -3,12 +3,12 @@ import wipeCache from './wipeCache'; import createScope from './scope'; import {getScopeVariable, setScope} from './globals'; import { - convertName, - onMockCreate, - onDisable, - onEnable, - addPlugin as addPluginAPI, - removePlugin as removePluginAPI + convertName, + onMockCreate, + onDisable, + onEnable, + addPlugin as addPluginAPI, + removePlugin as removePluginAPI } from './plugins'; import {resetMock, getMock, getAsyncMock, getAsyncModuleName, getAllMocks} from './mocks'; import ModuleMock from './mock'; @@ -17,8 +17,8 @@ let parentModule = getModuleParent(module); let mockScope = null; const scope = () => setScope(mockScope); const updateScope = (parentScope = null) => { - mockScope = createScope(parentScope, parentModule); - scope(); + mockScope = createScope(parentScope, parentModule); + scope(); }; updateScope(); @@ -31,25 +31,25 @@ updateScope(); * @return {ModuleMock} */ function mockModule(moduleName) { - scope(); - if(typeof moduleName === 'function'){ - return onMockCreate(new ModuleMock(getAsyncMock(moduleName, parentModule))); - } else { - const name = convertName(moduleName, parentModule); - resetMock(name); - return onMockCreate(new ModuleMock(getMock(name))); - } + scope(); + if (typeof moduleName === 'function') { + return onMockCreate(new ModuleMock(getAsyncMock(moduleName, parentModule))); + } else { + const name = convertName(moduleName, parentModule); + resetMock(name); + return onMockCreate(new ModuleMock(getMock(name))); + } } mockModule.getMock = (module) => { let moduleName = module; - if(typeof moduleName === 'function'){ + if (typeof moduleName === 'function') { moduleName = getAsyncModuleName(moduleName, parentModule); } else { moduleName = convertName(moduleName, parentModule); } const mock = getMock(moduleName); - if(mock) { + if (mock) { return new ModuleMock(mock) } return null; @@ -61,8 +61,8 @@ mockModule.getMock = (module) => { * @return {String} converted module name */ mockModule.resolve = (module) => { - scope(); - return convertName(module, parentModule); + scope(); + return convertName(module, parentModule); }; /** flags **/ @@ -74,20 +74,24 @@ mockModule.resolve = (module) => { * @param {Boolean} [options.noParentPassBy] disable allowing any module, with allowed parent */ mockModule.isolation = (options = {}) => { - mockScope.isolation = Object.assign({}, options); - return mockModule; + mockScope.isolation = Object.assign({}, options); + return mockModule; }; /** * Deactivates isolation */ mockModule.withoutIsolation = () => { - mockScope.isolation = false; - return mockModule; + mockScope.isolation = false; + return mockModule; }; mockModule.forceCacheClear = (mode) => { - mockScope.forceCacheClear = mode ? mode : true; + mockScope.forceCacheClear = mode !== undefined ? mode : true; +}; + +mockModule.setCacheControl = (mode) => { + mockScope.cacheControl = mode; }; /** @@ -95,12 +99,12 @@ mockModule.forceCacheClear = (mode) => { * @param {String|RegEx|Function} pattern */ mockModule.passBy = (pattern) => { - mockScope.passBy.push(pattern); - return mockModule; + mockScope.passBy.push(pattern); + return mockModule; }; mockModule.overrideEntryPoint = (parent) => { - mockScope.parentModule = parentModule = parent || getModuleParent(getModuleParent(module)); + mockScope.parentModule = parentModule = parent || getModuleParent(getModuleParent(module)); }; @@ -110,25 +114,27 @@ mockModule.overrideEntryPoint = (parent) => { * enabled rewiremock */ mockModule.enable = () => { - scope(); - Module.probeSyncModules(); - Module.overloadRequire(); - storeCache(); + scope(); + Module.probeSyncModules(); + Module.overloadRequire(); + storeCache(); + if (getScopeVariable('cacheControl') !== false) { wipeCache(); - onEnable(getAllMocks()); - return mockModule; + } + onEnable(getAllMocks()); + return mockModule; }; /** * disabled rewiremock */ mockModule.disable = () => { - scope(); - Module.restoreRequire(); - onDisable(getAllMocks()); - mockModule.withoutIsolation(); - mockModule.flush(); - return mockModule; + scope(); + Module.restoreRequire(); + onDisable(getAllMocks()); + mockModule.withoutIsolation(); + mockModule.flush(); + return mockModule; }; @@ -138,28 +144,28 @@ mockModule.disable = () => { * @param {Object|Function} overrides */ mockModule.proxy = (file, overrides = {}) => { - let result = 0; - - mockModule.inScope( () => { - const stubs = ( - typeof overrides === 'function' - ? overrides(ModuleMock.inlineConstructor) - : overrides - ) || {}; - - Object - .keys(stubs) - .forEach( key => mockModule(key).from(stubs[key])); - - mockModule.enable(); - if(typeof file === 'string') { - result = mockModule.requireActual(file); - } else { - result = file(); - } - mockModule.disable(); - }); - return result; + let result = 0; + + mockModule.inScope(() => { + const stubs = ( + typeof overrides === 'function' + ? overrides(ModuleMock.inlineConstructor) + : overrides + ) || {}; + + Object + .keys(stubs) + .forEach(key => mockModule(key).from(stubs[key])); + + mockModule.enable(); + if (typeof file === 'string') { + result = mockModule.requireActual(file); + } else { + result = file(); + } + mockModule.disable(); + }); + return result; }; /** @@ -187,18 +193,18 @@ mockModule.module = (importFunction, overrides = {}) => { * @param callback */ mockModule.inScope = (callback) => { - const currentScope = mockScope; - let error; - updateScope(currentScope); - try { - callback(); - } catch(e) { - error = e; - } + const currentScope = mockScope; + let error; + updateScope(currentScope); + try { + callback(); + } catch (e) { + error = e; + } - mockScope = currentScope; - if(error) throw error; - return mockModule; + mockScope = currentScope; + if (error) throw error; + return mockModule; }; @@ -209,29 +215,29 @@ mockModule.inScope = (callback) => { * @return {Promise} */ mockModule.around = (loader, createCallback) => { - return new Promise((resolve, reject) => { - const currentScope = mockScope; - updateScope(currentScope); - - const restore = () => { - mockModule.disable(); - mockScope = currentScope; - }; - - Promise.resolve(createCallback && createCallback(mockModule)) - .then(() => Module.probeAsyncModules()) - .then(() => mockModule.enable()) - .then(() => - Promise.resolve(loader()) - .then((mockedResult) => { - restore(); - resolve(mockedResult); - }, (err) => { - restore(); - reject(err) - }) - ); - }); + return new Promise((resolve, reject) => { + const currentScope = mockScope; + updateScope(currentScope); + + const restore = () => { + mockModule.disable(); + mockScope = currentScope; + }; + + Promise.resolve(createCallback && createCallback(mockModule)) + .then(() => Module.probeAsyncModules()) + .then(() => mockModule.enable()) + .then(() => + Promise.resolve(loader()) + .then((mockedResult) => { + restore(); + resolve(mockedResult); + }, (err) => { + restore(); + reject(err) + }) + ); + }); }; mockModule.stubFactory = factory => { @@ -240,13 +246,13 @@ mockModule.stubFactory = factory => { }; const storeCache = () => { - mockScope.requireCache = mockScope.requireCache || Object.assign({},require.cache); + mockScope.requireCache = mockScope.requireCache || Object.assign({}, require.cache); }; const restoreCache = () => { const oldCache = mockScope.requireCache; const newCache = require.cache; - if(oldCache) { + if (oldCache) { Object .keys(oldCache) //.filter(key => !newCache[key]) @@ -259,7 +265,7 @@ const restoreCache = () => { const swapCache = () => { const oldCache = mockScope.requireCache; const newCache = require.cache; - if(oldCache) { + if (oldCache) { Object .keys(newCache) .filter(key => !oldCache[key]) @@ -277,19 +283,19 @@ const swapCache = () => { * flushes all active overrides */ mockModule.flush = () => { - const forceCacheClear = getScopeVariable('forceCacheClear'); - // flush away soiled modules - wipeCache(mockScope.mockedModules); - mockScope.mockedModules = {}; - if(forceCacheClear) { - if (forceCacheClear !== 'nocache') { - // restore cache completely - swapCache(); - } - } else { - // merge caches - restoreCache(); + const forceCacheClear = getScopeVariable('forceCacheClear'); + // flush away soiled modules + wipeCache(mockScope.mockedModules); + mockScope.mockedModules = {}; + if (forceCacheClear) { + if (forceCacheClear !== 'nocache') { + // restore cache completely + swapCache(); } + } else { + // merge caches + restoreCache(); + } }; /** @@ -309,37 +315,37 @@ mockModule.importActual = (fileName) => Promise.resolve(this.requireActual(fileN * flushes anything */ mockModule.clear = () => { - updateScope(); - scope(); - mockModule.withoutIsolation(); - mockModule.flush(); + updateScope(); + scope(); + mockModule.withoutIsolation(); + mockModule.flush(); }; const cleanup = () => { - delete require.cache[require.resolve(__filename)]; + delete require.cache[require.resolve(__filename)]; }; const addPlugin = (plugin) => { - scope(); - addPluginAPI(plugin); + scope(); + addPluginAPI(plugin); }; const removePlugin = (plugin) => { - scope(); - removePluginAPI(plugin); + scope(); + removePluginAPI(plugin); }; mockModule.addPlugin = (plugin) => { - addPlugin(plugin); - return mockModule; + addPlugin(plugin); + return mockModule; }; export { - mockModule, - addPlugin, - removePlugin, - cleanup + mockModule, + addPlugin, + removePlugin, + cleanup }; diff --git a/src/wipeCache.js b/src/wipeCache.js index c45852d..40e80da 100644 --- a/src/wipeCache.js +++ b/src/wipeCache.js @@ -20,9 +20,14 @@ const resolver = (stubs, moduleName) => { relativeWipeCheck(stubs, moduleName); }; -const wipeCache = (primaryCache = {}) => { - wipe(primaryCache, primaryResolver); - wipe(getAllMocks(), resolver); +const wipeCache = (primaryCache) => { + if(primaryCache) { + // post clean + wipe(primaryCache, primaryResolver); + } else { + // pre clean + wipe(getAllMocks(), resolver); + } }; export default wipeCache; \ No newline at end of file From 9554a91d84198674c215dfd0d79904bc3d8947e6 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Thu, 7 May 2020 16:26:10 +1000 Subject: [PATCH 6/7] linting --- src/executor.js | 1 + src/plugins/webpack-alias.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/executor.js b/src/executor.js index cbce109..ed624b2 100644 --- a/src/executor.js +++ b/src/executor.js @@ -185,6 +185,7 @@ function restoreESModuleState(dest, original) { value: true }); } catch (e) { + // eslint-disable-next-line no-console console.error(e); } } diff --git a/src/plugins/webpack-alias.js b/src/plugins/webpack-alias.js index deb8cdc..550bfbb 100644 --- a/src/plugins/webpack-alias.js +++ b/src/plugins/webpack-alias.js @@ -1,5 +1,5 @@ import {readAliases, processFile} from './common/aliases' -import createPlugin, {standardWipeCheck} from './_common'; +import createPlugin from './_common'; let settings = null; From b13cd4b19660b6cb4570919b27a27450c63e97b1 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Thu, 7 May 2020 16:27:01 +1000 Subject: [PATCH 7/7] 3.14.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e794301..6d08f79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rewiremock", - "version": "3.13.9", + "version": "3.14.0", "description": "Advanced dependency mocking device.", "main": "lib/index.js", "jsnext:main": "es/index.js",