From a33dd76e4fc8bda804fe420fb36f92598c6d5aa1 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Thu, 1 Jun 2017 09:58:07 -0700 Subject: [PATCH] metro-bundler: fix integration test for OSS Summary: This should fix the e2e test for OSS. Reviewed By: cpojer Differential Revision: D5164267 fbshipit-source-id: d858b4811f0eb80ca7363177f4f384b66af13994 --- package.json | 2 +- .../__tests__/basic_bundle-test.js | 43 ----- .../__snapshots__/basic_bundle-test.js.snap | 165 ++++++++---------- .../__tests__/basic_bundle-test.js | 89 ++++++++++ .../integration_tests/basic_bundle/Bar.js | 0 .../integration_tests/basic_bundle/Foo.js | 0 .../basic_bundle/TestBundle.js | 0 .../integration_tests/basic_bundle/test.png | Bin 8 files changed, 166 insertions(+), 133 deletions(-) delete mode 100644 packager/integration_tests/__tests__/basic_bundle-test.js rename packager/{ => src}/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap (92%) create mode 100644 packager/src/integration_tests/__tests__/basic_bundle-test.js rename packager/{ => src}/integration_tests/basic_bundle/Bar.js (100%) rename packager/{ => src}/integration_tests/basic_bundle/Foo.js (100%) rename packager/{ => src}/integration_tests/basic_bundle/TestBundle.js (100%) rename packager/{ => src}/integration_tests/basic_bundle/test.png (100%) diff --git a/package.json b/package.json index 7e2c6dc28..b48109340 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "/node_modules/", "/website/", "local-cli/templates/", - "packager/integration_tests/__tests__/basic_bundle-test.js" + "packager/src/integration_tests/__tests__/basic_bundle-test.js" ], "haste": { "defaultPlatform": "ios", diff --git a/packager/integration_tests/__tests__/basic_bundle-test.js b/packager/integration_tests/__tests__/basic_bundle-test.js deleted file mode 100644 index a42675bf0..000000000 --- a/packager/integration_tests/__tests__/basic_bundle-test.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright (c) 2015-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. - * - * @format - */ - -'use strict'; - -jest.disableAutomock(); -jest.useRealTimers(); - -const Packager = require('../..'); - -const path = require('path'); - -jasmine.DEFAULT_TIMEOUT_INTERVAL = 30 * 1000; - -const INPUT_PATH = path.resolve(__dirname, '../basic_bundle'); -const POLYFILLS_PATH = path.resolve(__dirname, '../../src/Resolver/polyfills'); - -describe('basic_bundle', () => { - it('bundles package as expected', async () => { - const bundle = await Packager.buildBundle( - { - projectRoots: [INPUT_PATH, POLYFILLS_PATH], - transformCache: Packager.TransformCaching.none(), - transformModulePath: require.resolve('../../transformer'), - }, - { - dev: false, - entryFile: path.join(INPUT_PATH, 'TestBundle.js'), - platform: 'ios', - }, - ); - const absPathRe = new RegExp(INPUT_PATH, 'g'); - expect(bundle.getSource().replace(absPathRe, '')).toMatchSnapshot(); - }); -}); diff --git a/packager/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap b/packager/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap similarity index 92% rename from packager/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap rename to packager/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap index 4a369dbd7..7c3321969 100644 --- a/packager/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap +++ b/packager/src/integration_tests/__tests__/__snapshots__/basic_bundle-test.js.snap @@ -143,78 +143,74 @@ function moduleThrewError(id, error) { } if (__DEV__) { - var createHotReloadingObject; + _require.Systrace = { beginEvent: function beginEvent() {}, endEvent: function endEvent() {} }; - (function () { - _require.Systrace = { beginEvent: function beginEvent() {}, endEvent: function endEvent() {} }; - - createHotReloadingObject = function createHotReloadingObject() { - var hot = { - acceptCallback: null, - accept: function accept(callback) { - hot.acceptCallback = callback; - } - }; - return hot; + var createHotReloadingObject = function createHotReloadingObject() { + var hot = { + acceptCallback: null, + accept: function accept(callback) { + hot.acceptCallback = callback; + } }; + return hot; + }; - var acceptAll = function acceptAll(dependentModules, inverseDependencies) { - if (!dependentModules || dependentModules.length === 0) { - return true; - } + var acceptAll = function acceptAll(dependentModules, inverseDependencies) { + if (!dependentModules || dependentModules.length === 0) { + return true; + } - var notAccepted = dependentModules.filter(function (module) { - return !accept(module, undefined, inverseDependencies); - }); + var notAccepted = dependentModules.filter(function (module) { + return !_accept(module, undefined, inverseDependencies); + }); - var parents = []; - for (var i = 0; i < notAccepted.length; i++) { - if (inverseDependencies[notAccepted[i]].length === 0) { - return false; - } - - parents.push.apply(parents, babelHelpers.toConsumableArray(inverseDependencies[notAccepted[i]])); - } - - return acceptAll(parents, inverseDependencies); - }; - - var accept = function accept(id, factory, inverseDependencies) { - var mod = modules[id]; - - if (!mod && factory) { - define(factory, id); - return true; - } - - var hot = mod.hot; - - if (!hot) { - console.warn('Cannot accept module because Hot Module Replacement ' + 'API was not installed.'); + var parents = []; + for (var i = 0; i < notAccepted.length; i++) { + if (inverseDependencies[notAccepted[i]].length === 0) { return false; } - if (factory) { - mod.factory = factory; + parents.push.apply(parents, babelHelpers.toConsumableArray(inverseDependencies[notAccepted[i]])); + } + + return acceptAll(parents, inverseDependencies); + }; + + var _accept = function _accept(id, factory, inverseDependencies) { + var mod = modules[id]; + + if (!mod && factory) { + define(factory, id); + return true; + } + + var hot = mod.hot; + + if (!hot) { + console.warn('Cannot accept module because Hot Module Replacement ' + 'API was not installed.'); + return false; + } + + if (factory) { + mod.factory = factory; + } + mod.hasError = false; + mod.isInitialized = false; + _require(id); + + if (hot.acceptCallback) { + hot.acceptCallback(); + return true; + } else { + if (!inverseDependencies) { + throw new Error('Undefined \`inverseDependencies\`'); } - mod.hasError = false; - mod.isInitialized = false; - _require(id); - if (hot.acceptCallback) { - hot.acceptCallback(); - return true; - } else { - if (!inverseDependencies) { - throw new Error('Undefined \`inverseDependencies\`'); - } + return acceptAll(inverseDependencies[id], inverseDependencies); + } + }; - return acceptAll(inverseDependencies[id], inverseDependencies); - } - }; - - global.__accept = accept; - })(); + global.__accept = _accept; } })(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this); (function(global) { @@ -748,17 +744,15 @@ if (Number.MIN_SAFE_INTEGER === undefined) { }); } if (!Number.isNaN) { - (function () { - var globalIsNaN = global.isNaN; - Object.defineProperty(Number, 'isNaN', { - configurable: true, - enumerable: false, - value: function isNaN(value) { - return typeof value === 'number' && globalIsNaN(value); - }, - writable: true - }); - })(); + var globalIsNaN = global.isNaN; + Object.defineProperty(Number, 'isNaN', { + configurable: true, + enumerable: false, + value: function isNaN(value) { + return typeof value === 'number' && globalIsNaN(value); + }, + writable: true + }); } })(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this); (function(global) { @@ -1255,34 +1249,27 @@ __d(/* /TestBundle.js */function(global, require, module, exports) { 'use strict'; -var Bar = require(12 ); // 12 = ./Bar -var Foo = require(13 ); // 13 = ./Foo - -module.exports = { Foo: Foo, Bar: Bar }; +module.exports = { Foo: require(12 ), Bar: require(15 ) }; // 15 = ./Bar // 12 = ./Foo }, 0); -__d(/* /Bar.js */function(global, require, module, exports) { - -'use strict'; - -var Foo = require(13 ); // 13 = ./Foo - -module.exports = { type: 'bar', foo: Foo.type }; -}, 12); __d(/* /Foo.js */function(global, require, module, exports) { 'use strict'; -var asset = require(14 ); // 14 = ./test.png - -module.exports = { type: 'foo', asset: asset }; +module.exports = { type: 'foo', asset: require(13 ) }; // 13 = ./test.png +}, 12); +__d(/* /test.png */function(global, require, module, exports) {module.exports=require(14 ).registerAsset({\\"__packager_asset\\":true,\\"httpServerLocation\\":\\"/assets\\",\\"width\\":8,\\"height\\":8,\\"scales\\":[1],\\"hash\\":\\"77d45c1f7fa73c0f6c444a830dc42f67\\",\\"name\\":\\"test\\",\\"type\\":\\"png\\"}); // 14 = react-native/Libraries/Image/AssetRegistry }, 13); -__d(/* /test.png */function(global, require, module, exports) {module.exports=require(15 ).registerAsset({\\"__packager_asset\\":true,\\"httpServerLocation\\":\\"/assets\\",\\"width\\":8,\\"height\\":8,\\"scales\\":[1],\\"hash\\":\\"77d45c1f7fa73c0f6c444a830dc42f67\\",\\"name\\":\\"test\\",\\"type\\":\\"png\\"}); // 15 = react-native/Libraries/Image/AssetRegistry -}, 14); __d(/* /node_modules/react-native/Libraries/Image/AssetRegistry.js */function(global, require, module, exports) { 'use strict'; module.export = {}; +}, 14); +__d(/* /Bar.js */function(global, require, module, exports) { + +'use strict'; + +module.exports = { type: 'bar', foo: require(12 ).type }; // 12 = ./Foo }, 15); ;require(0);" `; diff --git a/packager/src/integration_tests/__tests__/basic_bundle-test.js b/packager/src/integration_tests/__tests__/basic_bundle-test.js new file mode 100644 index 000000000..fce916d08 --- /dev/null +++ b/packager/src/integration_tests/__tests__/basic_bundle-test.js @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2015-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. + * + * @format + */ + +'use strict'; + +jest.disableAutomock(); +jest.useRealTimers(); + +jest.dontMock('fs'); +jest.dontMock('graceful-fs'); + +/** + * Don't waste time creating a worker-farm from jest-haste-map, use the function + * directly instead. + */ +jest.mock('worker-farm', () => { + function workerFarm(opts, workerPath, methodNames) { + return require(workerPath); + } + workerFarm.end = () => {}; + return workerFarm; +}); + +/** + * We replace the farm by a simple require, so that the worker sources are + * transformed and managed by jest. + */ +jest.mock('../../worker-farm', () => { + let ended = false; + function workerFarm(opts, workerPath, methodNames) { + const {Readable} = require('stream'); + const methods = {}; + const worker = require(workerPath); + methodNames.forEach(name => { + methods[name] = function() { + if (ended) { + throw new Error('worker farm was ended'); + } + return worker[name].apply(null, arguments); + }; + }); + return { + stdout: new Readable({read() {}}), + stderr: new Readable({read() {}}), + methods, + }; + } + workerFarm.end = () => { + ended = true; + }; + return workerFarm; +}); + +const Packager = require('../..'); + +const path = require('path'); + +jasmine.DEFAULT_TIMEOUT_INTERVAL = 30 * 1000; + +const INPUT_PATH = path.resolve(__dirname, '../basic_bundle'); +const POLYFILLS_PATH = path.resolve(__dirname, '../../Resolver/polyfills'); + +describe('basic_bundle', () => { + it('bundles package as expected', async () => { + const bundle = await Packager.buildBundle( + { + projectRoots: [INPUT_PATH, POLYFILLS_PATH], + transformCache: Packager.TransformCaching.none(), + transformModulePath: require.resolve('../../transformer'), + nonPersistent: true, + }, + { + dev: false, + entryFile: path.join(INPUT_PATH, 'TestBundle.js'), + platform: 'ios', + }, + ); + const absPathRe = new RegExp(INPUT_PATH, 'g'); + expect(bundle.getSource().replace(absPathRe, '')).toMatchSnapshot(); + }); +}); diff --git a/packager/integration_tests/basic_bundle/Bar.js b/packager/src/integration_tests/basic_bundle/Bar.js similarity index 100% rename from packager/integration_tests/basic_bundle/Bar.js rename to packager/src/integration_tests/basic_bundle/Bar.js diff --git a/packager/integration_tests/basic_bundle/Foo.js b/packager/src/integration_tests/basic_bundle/Foo.js similarity index 100% rename from packager/integration_tests/basic_bundle/Foo.js rename to packager/src/integration_tests/basic_bundle/Foo.js diff --git a/packager/integration_tests/basic_bundle/TestBundle.js b/packager/src/integration_tests/basic_bundle/TestBundle.js similarity index 100% rename from packager/integration_tests/basic_bundle/TestBundle.js rename to packager/src/integration_tests/basic_bundle/TestBundle.js diff --git a/packager/integration_tests/basic_bundle/test.png b/packager/src/integration_tests/basic_bundle/test.png similarity index 100% rename from packager/integration_tests/basic_bundle/test.png rename to packager/src/integration_tests/basic_bundle/test.png