diff --git a/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js new file mode 100644 index 00000000..1900ed85 --- /dev/null +++ b/packages/metro-bundler/src/lib/__tests__/GlobalTransformCache-test.js @@ -0,0 +1,91 @@ +/** + * Copyright (c) 2016-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +jest.disableAutomock(); +jest.useRealTimers(); + +const fetchMock = jest.fn(); +jest.mock('node-fetch', () => fetchMock); + +const GlobalTransformCache = require('../GlobalTransformCache'); +const FetchError = require('node-fetch/lib/fetch-error'); + +async function fetchResultURIs(keys: Array): Promise> { + return new Map(keys.map(key => [key, `http://globalcache.com/${key}`])); +} + +async function fetchResultFromURI(uri: string): Promise { + return { + code: `/* code from ${uri} */`, + dependencies: [], + dependencyOffsets: [], + }; +} + +describe('GlobalTransformCache', () => { + + it('fetches results', async () => { + const cache = new GlobalTransformCache(fetchResultURIs, fetchResultFromURI, null, [ + {dev: true, minify: false, platform: 'ios'}, + ]); + const transformOptions = { + dev: true, + minify: false, + platform: 'ios', + transform: {projectRoots: [__dirname]}, + }; + const result = await Promise.all([cache.fetch({ + filePath: 'foo.js', + sourceCode: '/* beep */', + getTransformCacheKey: () => 'abcd', + transformOptions, + }), cache.fetch({ + filePath: 'bar.js', + sourceCode: '/* boop */', + getTransformCacheKey: () => 'abcd', + transformOptions, + })]); + expect(result).toMatchSnapshot(); + }); + + describe('fetchResultFromURI', () => { + + const defaultFetchMockImpl = async uri => ({ + status: 200, + json: async () => ({ + code: `/* code from ${uri} */`, + dependencies: [], + dependencyOffsets: [], + }), + }); + + beforeEach(() => { + fetchMock.mockReset(); + }); + + it('fetches result', async () => { + fetchMock.mockImplementation(defaultFetchMockImpl); + const result = await GlobalTransformCache.fetchResultFromURI('http://globalcache.com/foo'); + expect(result).toMatchSnapshot(); + }); + + it('retries once on timeout', async () => { + fetchMock.mockImplementation(async uri => { + fetchMock.mockImplementation(defaultFetchMockImpl); + throw new FetchError('timeout!', 'request-timeout'); + }); + const result = await GlobalTransformCache.fetchResultFromURI('http://globalcache.com/foo'); + expect(result).toMatchSnapshot(); + }); + + }); + +}); diff --git a/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap b/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap new file mode 100644 index 00000000..c0fdf1ad --- /dev/null +++ b/packages/metro-bundler/src/lib/__tests__/__snapshots__/GlobalTransformCache-test.js.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GlobalTransformCache fetchResultFromURI fetches result 1`] = ` +Object { + "code": "/* code from http://globalcache.com/foo */", + "dependencies": Array [], + "dependencyOffsets": Array [], +} +`; + +exports[`GlobalTransformCache fetchResultFromURI retries once on timeout 1`] = ` +Object { + "code": "/* code from http://globalcache.com/foo */", + "dependencies": Array [], + "dependencyOffsets": Array [], +} +`; + +exports[`GlobalTransformCache fetches results 1`] = ` +Array [ + Object { + "code": "/* code from http://globalcache.com/2ad175cb80ae79fd33b914bfb392fb6742982d2a-foo.js */", + "dependencies": Array [], + "dependencyOffsets": Array [], + }, + Object { + "code": "/* code from http://globalcache.com/d6c0a1a4199d572ab68b36c07d0d68607eebb131-bar.js */", + "dependencies": Array [], + "dependencyOffsets": Array [], + }, +] +`;