From 1de20b01355fba7b1763675dc357a8cc002678fa Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Wed, 27 Jan 2016 18:46:23 -0800 Subject: [PATCH] Limit mocks shared with a resolution response Reviewed By: davidaurelio Differential Revision: D2872340 fb-gh-sync-id: a76b580ff670115cd4bd0098e7b9f31110239369 --- .../DependencyGraph/ResolutionRequest.js | 19 ++++++++++--------- .../__tests__/DependencyGraph-test.js | 5 +++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js b/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js index ebe7c39a..58dcc7d1 100644 --- a/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js +++ b/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js @@ -104,10 +104,9 @@ class ResolutionRequest { } getOrderedDependencies(response, mocksPattern) { - return this._getAllMocks(mocksPattern).then(mocks => { - response.setMocks(mocks); - + return this._getAllMocks(mocksPattern).then(allMocks => { const entry = this._moduleCache.getModule(this._entryPath); + const mocks = Object.create(null); const visited = Object.create(null); visited[entry.hash()] = true; @@ -118,13 +117,14 @@ class ResolutionRequest { depNames.map(name => this.resolveDependency(mod, name)) ).then((dependencies) => [depNames, dependencies]) ).then(([depNames, dependencies]) => { - if (mocks) { + if (allMocks) { return mod.getName().then(name => { - if (mocks[name]) { + if (allMocks[name]) { const mockModule = - this._moduleCache.getModule(mocks[name]); + this._moduleCache.getModule(allMocks[name]); depNames.push(name); dependencies.push(mockModule); + mocks[name] = allMocks[name]; } return [depNames, dependencies]; }); @@ -141,8 +141,9 @@ class ResolutionRequest { // module backing them. If a dependency cannot be found but there // exists a mock with the desired ID, resolve it and add it as // a dependency. - if (mocks && mocks[name]) { - const mockModule = this._moduleCache.getModule(mocks[name]); + if (allMocks && allMocks[name]) { + const mockModule = this._moduleCache.getModule(allMocks[name]); + mocks[name] = allMocks[name]; return filteredPairs.push([name, mockModule]); } @@ -172,7 +173,7 @@ class ResolutionRequest { }); }; - return collect(entry); + return collect(entry).then(() => response.setMocks(mocks)); }); } diff --git a/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js b/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js index 6c88acb6..464985f6 100644 --- a/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js @@ -4117,11 +4117,11 @@ describe('DependencyGraph', function() { return dgraph.getDependencies('/root/index.js') .then(response => response.finalize()) .then(response => { - expect(response.mocks).toBe(null); + expect(response.mocks).toEqual({}); }); }); - pit('retrieves a list of all mocks in the system', () => { + pit('retrieves a list of all required mocks', () => { var root = '/root'; fs.__setMockFilesystem({ 'root': { @@ -4133,6 +4133,7 @@ describe('DependencyGraph', function() { '/**', ' * @providesModule b', ' */', + 'require("A");', ].join('\n'), }, });