From 0fa1c5f0f1e4f7acb16d14ad6917bef79fc8c206 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Wed, 12 Apr 2017 09:34:31 -0700 Subject: [PATCH] Use escape hatch for mocked modules Reviewed By: davidaurelio Differential Revision: D4869222 fbshipit-source-id: d2cf66296a26710630f0d0f6c8efc7ff3ed97bc5 --- .../src/lib/__tests__/GlobalTransformCache-test.js | 12 ++++++------ .../src/lib/__tests__/TransformCache-test.js | 10 +++++----- .../src/node-haste/__tests__/DependencyGraph-test.js | 9 +++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js index 94039e8b..ec077d1e 100644 --- a/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js +++ b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js @@ -12,8 +12,8 @@ jest.disableAutomock(); jest.useRealTimers(); -const fetchMock = jest.fn(); -jest.mock('node-fetch', () => fetchMock); +const mockFetch = jest.fn(); +jest.mock('node-fetch', () => mockFetch); const {URIBasedGlobalTransformCache} = require('../GlobalTransformCache'); const FetchError = require('node-fetch/lib/fetch-error'); @@ -82,19 +82,19 @@ describe('GlobalTransformCache', () => { }); beforeEach(() => { - fetchMock.mockReset(); + mockFetch.mockReset(); }); it('fetches result', async () => { - fetchMock.mockImplementation(defaultFetchMockImpl); + mockFetch.mockImplementation(defaultFetchMockImpl); const result = await URIBasedGlobalTransformCache .fetchResultFromURI('http://globalcache.com/foo'); expect(result).toMatchSnapshot(); }); it('retries once on timeout', async () => { - fetchMock.mockImplementation(async uri => { - fetchMock.mockImplementation(defaultFetchMockImpl); + mockFetch.mockImplementation(async uri => { + mockFetch.mockImplementation(defaultFetchMockImpl); throw new FetchError('timeout!', 'request-timeout'); }); const result = await URIBasedGlobalTransformCache diff --git a/packages/metro-bundler/src/lib/__tests__/TransformCache-test.js b/packages/metro-bundler/src/lib/__tests__/TransformCache-test.js index 9f34c483..e9bab8eb 100644 --- a/packages/metro-bundler/src/lib/__tests__/TransformCache-test.js +++ b/packages/metro-bundler/src/lib/__tests__/TransformCache-test.js @@ -21,14 +21,14 @@ const imurmurhash = require('imurmurhash'); const crypto = require('crypto'); const jsonStableStringify = require('json-stable-stringify'); -const memoryFS = new Map(); +const mockFS = new Map(); jest.mock('fs', () => ({ readFileSync(filePath) { - return memoryFS.get(filePath); + return mockFS.get(filePath); }, unlinkSync(filePath) { - memoryFS.delete(filePath); + mockFS.delete(filePath); }, readdirSync(dirPath) { // Not required for it to work. @@ -38,7 +38,7 @@ jest.mock('fs', () => ({ jest.mock('write-file-atomic', () => ({ sync(filePath, data) { - memoryFS.set(filePath, data.toString()); + mockFS.set(filePath, data.toString()); }, })); @@ -56,7 +56,7 @@ describe('TransformCache', () => { beforeEach(() => { jest.resetModules(); - memoryFS.clear(); + mockFS.clear(); TransformCache = require('../TransformCache'); }); diff --git a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js index f55ee0a9..489be4ba 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js @@ -19,9 +19,10 @@ jest ; // This doesn't have state, and it's huge (Babel) so it's much faster to -// require it only once. -const extractDependencies = require('../../JSTransformer/worker/extract-dependencies'); -jest.mock('../../JSTransformer/worker/extract-dependencies', () => extractDependencies); +// require it only once. The variable name is prefixed with "mock" as an escape-hatch +// for babel-plugin-jest-hoist. +const mockExtractDependencies = require('../../JSTransformer/worker/extract-dependencies'); +jest.mock('../../JSTransformer/worker/extract-dependencies', () => mockExtractDependencies); jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; @@ -119,7 +120,7 @@ describe('DependencyGraph', function() { return new Promise(resolve => { let deps = {dependencies: [], dependencyOffsets: []}; if (!module.path.endsWith('.json')) { - deps = extractDependencies(sourceCode); + deps = mockExtractDependencies(sourceCode); } resolve({...deps, code: sourceCode}); });