From 6a5f3b13ba93c59b86eb44ef198fbf4cb06e1375 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Fri, 8 Dec 2017 19:46:50 +1100 Subject: [PATCH 1/3] correct cache behavior --- _tests/mock.spec.js | 15 ++++++++++++++- src/mockModule.js | 2 +- src/plugins/relative.js | 9 ++++++--- src/wipeCache.js | 3 ++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/_tests/mock.spec.js b/_tests/mock.spec.js index 6807521..ad9b6c7 100644 --- a/_tests/mock.spec.js +++ b/_tests/mock.spec.js @@ -189,7 +189,20 @@ describe('rewiremock ', () => { .then(mocked => expect(mocked()).to.be.equal('>+!mock')); }); - it('should mock all due to callThrough mocked : ', () => { + it('should mock all due to callThrough mocked / async : ', () => { + return rewiremock.around(() => require('./lib/c/barbaz.js'), + () => { + //addPlugin(nodePlugin); + rewiremock(() => import('./lib/c/baz')) + .with(() => 'mock') + .calledFromMock(); + rewiremock(() => import('./lib/c/bar')) + .callThrough(); + }) + .then(mocked => expect(mocked()).to.be.equal('>+mockmock')); + }); + + it('should mock all due to callThrough mocked / sync: ', () => { return rewiremock.around(() => require('./lib/c/barbaz.js'), () => { //addPlugin(nodePlugin); diff --git a/src/mockModule.js b/src/mockModule.js index 637f438..16e3c1b 100644 --- a/src/mockModule.js +++ b/src/mockModule.js @@ -214,8 +214,8 @@ mockModule.around = (loader, createCallback) => { }; Promise.resolve(createCallback && createCallback(mockModule)) - .then(() => mockModule.enable()) .then(() => Module.probeAsyncModules()) + .then(() => mockModule.enable()) .then(() => Promise.resolve(loader()) .then((mockedResult) => { diff --git a/src/plugins/relative.js b/src/plugins/relative.js index 64280cc..89ffa2a 100644 --- a/src/plugins/relative.js +++ b/src/plugins/relative.js @@ -1,12 +1,15 @@ import createPlugin, {YES, PASS, NO} from './_common'; import {inParents} from '../module'; +import { extensions } from "../_common"; const trimKey = (key) => key[0] == '.' ? trimKey(key.substr(1)) : key; -const relativeWipeCheck = (stubs, moduleName) => { +export const relativeWipeCheck = (stubs, moduleName) => { if (Object .keys(stubs) - .find(key => moduleName.indexOf(trimKey(key)) >= 0) + .find(key => + extensions.find( ext => moduleName.endsWith(trimKey(key+ext))) + ) ) { return YES; } @@ -21,7 +24,7 @@ const shouldMock = (mock, request, parent, topModule) => { const plugin = createPlugin({ fileNameTransformer, - wipeCheck, + //wipeCheck, shouldMock, name: 'relative' diff --git a/src/wipeCache.js b/src/wipeCache.js index c52137a..f1d8e30 100644 --- a/src/wipeCache.js +++ b/src/wipeCache.js @@ -1,5 +1,6 @@ import {getAllMocks} from './mocks'; import {shouldWipe} from './plugins' +import {relativeWipeCheck} from "./plugins/relative"; // which one? export const wipe = module.hot @@ -14,7 +15,7 @@ const resolver = (stubs, moduleName) => { if (moduleName.indexOf('\.node') > -1) { return false; } - return shouldWipe(stubs, moduleName); + return shouldWipe(stubs, moduleName) || primaryResolver(stubs, moduleName) || relativeWipeCheck(stubs, moduleName); }; const wipeCache = (primaryCache = {}) => { From 6c8e630767b8fab48ef6a3c3d5e540bdf3862af4 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Fri, 8 Dec 2017 20:01:08 +1100 Subject: [PATCH 2/3] linting --- src/executor.js | 1 + src/plugins/relative.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/executor.js b/src/executor.js index cca42fb..63622bd 100644 --- a/src/executor.js +++ b/src/executor.js @@ -67,6 +67,7 @@ function mockResult(name, mock, data) { if (mock.matchOrigin) { const matchResult = matchOrigin(mock.original, data, name, '%mock%', {noFunctionCompare: true}) if (matchResult) { + // eslint-disable-next-line no-console matchResult.forEach(line => console.error(line)); throw new Error('Rewiremock: provided mocks does not match ' + name); } diff --git a/src/plugins/relative.js b/src/plugins/relative.js index 89ffa2a..be47bf3 100644 --- a/src/plugins/relative.js +++ b/src/plugins/relative.js @@ -16,7 +16,7 @@ export const relativeWipeCheck = (stubs, moduleName) => { }; const fileNameTransformer = (fileName/*, module*/) => fileName; -const wipeCheck = (stubs, moduleName) => relativeWipeCheck(stubs, moduleName); +//const wipeCheck = (stubs, moduleName) => relativeWipeCheck(stubs, moduleName); const shouldMock = (mock, request, parent, topModule) => { return inParents(parent, topModule) ? PASS : NO; From 02dfdb0acddd3eb4d0ddb70eb6eb65af3b977a4f Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Fri, 8 Dec 2017 20:42:27 +1100 Subject: [PATCH 3/3] 3.4.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7ee7f2e..ad9028d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rewiremock", - "version": "3.4.0", + "version": "3.4.1", "description": "Easy and es6 compatible dependency mocking tool.", "main": "lib/index.js", "jsnext:main": "es/index.js",