diff --git a/Libraries/Animated/__tests__/Animated-test.js b/Libraries/Animated/__tests__/Animated-test.js index 5f24fc546fc29f..434211e805aea4 100644 --- a/Libraries/Animated/__tests__/Animated-test.js +++ b/Libraries/Animated/__tests__/Animated-test.js @@ -98,9 +98,10 @@ describe('Animated', () => { it('stops animation when detached', () => { - // jest environment doesn't have requestAnimationFrame :( - window.requestAnimationFrame = jest.genMockFunction(); - window.cancelAnimationFrame = jest.genMockFunction(); + // jest environment doesn't have cancelAnimationFrame :( + if (!window.cancelAnimationFrame) { + window.cancelAnimationFrame = jest.genMockFunction(); + } var anim = new Animated.Value(0); var callback = jest.genMockFunction(); diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index cd1cbfc05e0cc5..9c81dcdc4c35d6 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -12,10 +12,10 @@ jest .dontMock('AssetRegistry') .dontMock('../resolveAssetSource'); -var AssetRegistry; -var Platform; -var SourceCode; -var resolveAssetSource; +var AssetRegistry = require('AssetRegistry'); +var Platform = require('Platform'); +var NativeModules = require('NativeModules'); +var resolveAssetSource = require('../resolveAssetSource'); function expectResolvesAsset(input, expectedSource) { var assetId = AssetRegistry.registerAsset(input); @@ -26,10 +26,6 @@ describe('resolveAssetSource', () => { beforeEach(() => { jest.resetModuleRegistry(); __DEV__ = true; - AssetRegistry = require('AssetRegistry'); - Platform = require('Platform'); - SourceCode = require('NativeModules').SourceCode; - resolveAssetSource = require('../resolveAssetSource'); }); it('returns same source for simple static and network images', () => { @@ -64,7 +60,8 @@ describe('resolveAssetSource', () => { describe('bundle was loaded from network (DEV)', () => { beforeEach(() => { - SourceCode.scriptURL = 'http://10.0.0.1:8081/main.bundle'; + NativeModules.SourceCode.scriptURL = + 'http://10.0.0.1:8081/main.bundle'; Platform.OS = 'ios'; }); @@ -110,7 +107,8 @@ describe('resolveAssetSource', () => { describe('bundle was loaded from file (PROD) on iOS', () => { beforeEach(() => { - SourceCode.scriptURL = 'file:///Path/To/Simulator/main.bundle'; + NativeModules.SourceCode.scriptURL = + 'file:///Path/To/Simulator/main.bundle'; __DEV__ = false; Platform.OS = 'ios'; }); @@ -137,7 +135,8 @@ describe('resolveAssetSource', () => { describe('bundle was loaded from file (PROD) on Android', () => { beforeEach(() => { - SourceCode.scriptURL = 'file:///Path/To/Simulator/main.bundle'; + NativeModules.SourceCode.scriptURL = + 'file:///Path/To/Simulator/main.bundle'; __DEV__ = false; Platform.OS = 'android'; }); diff --git a/jestSupport/env.js b/jestSupport/env.js index 160f26766bfec9..ee29f626a5f54e 100644 --- a/jestSupport/env.js +++ b/jestSupport/env.js @@ -9,6 +9,3 @@ 'use strict'; global.__DEV__ = true; -global.setImmediate = global.setImmediate || function(fn) { - return setTimeout(fn, 0); -}; diff --git a/package.json b/package.json index 8c3236dbfe30ea..3ec4564dfb5cd0 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,7 @@ "scriptPreprocessor": "jestSupport/preprocessor.js", "setupEnvScriptFile": "jestSupport/env.js", "testPathIgnorePatterns": [ - "/node_modules/", - "packager/react-packager/src/Activity/" + "/node_modules/" ], "testFileExtensions": [ "js" @@ -38,7 +37,7 @@ "jestSupport" ], "scripts": { - "test": "jest", + "test": "NODE_ENV=test jest", "lint": "node linter.js Examples/ Libraries/", "start": "./packager/packager.sh || true" }, diff --git a/packager/react-packager/src/Activity/__tests__/Activity-test.js b/packager/react-packager/src/Activity/__tests__/Activity-test.js index 90ac43e8567ed1..39d222cea0714a 100644 --- a/packager/react-packager/src/Activity/__tests__/Activity-test.js +++ b/packager/react-packager/src/Activity/__tests__/Activity-test.js @@ -10,13 +10,13 @@ jest.autoMockOff(); +var Activity = require('../'); + describe('Activity', () => { const origConsoleLog = console.log; - let Activity; beforeEach(() => { console.log = jest.genMockFn(); - Activity = require('../'); jest.runOnlyPendingTimers(); }); @@ -68,9 +68,7 @@ describe('Activity', () => { expect(() => { Activity.endEvent(eid); - }).toThrow('event(3) has already ended!'); - - jest.runOnlyPendingTimers(); + }).toThrow('event(1) has already ended!'); }); }); diff --git a/packager/react-packager/src/AssetServer/__tests__/AssetServer-test.js b/packager/react-packager/src/AssetServer/__tests__/AssetServer-test.js index d36889149388cb..6c46b3ecf4ebe3 100644 --- a/packager/react-packager/src/AssetServer/__tests__/AssetServer-test.js +++ b/packager/react-packager/src/AssetServer/__tests__/AssetServer-test.js @@ -11,17 +11,11 @@ jest const Promise = require('promise'); -describe('AssetServer', () => { - let AssetServer; - let crypto; - let fs; - - beforeEach(() => { - AssetServer = require('../'); - crypto = require('crypto'); - fs = require('fs'); - }); +var AssetServer = require('../'); +var crypto = require('crypto'); +var fs = require('fs'); +describe('AssetServer', () => { describe('assetServer.get', () => { pit('should work for the simple case', () => { const server = new AssetServer({ diff --git a/packager/react-packager/src/Bundler/__tests__/Bundle-test.js b/packager/react-packager/src/Bundler/__tests__/Bundle-test.js index 74d18924013608..33b6646ef905e8 100644 --- a/packager/react-packager/src/Bundler/__tests__/Bundle-test.js +++ b/packager/react-packager/src/Bundler/__tests__/Bundle-test.js @@ -12,14 +12,14 @@ jest.autoMockOff(); var SourceMapGenerator = require('source-map').SourceMapGenerator; +var Bundle = require('../Bundle'); +var ModuleTransport = require('../../lib/ModuleTransport'); +var UglifyJS = require('uglify-js'); + describe('Bundle', function() { - var ModuleTransport; - var Bundle; var bundle; beforeEach(function() { - Bundle = require('../Bundle'); - ModuleTransport = require('../../lib/ModuleTransport'); bundle = new Bundle('test_url'); bundle.getSourceMap = jest.genMockFn().mockImpl(function() { return 'test-source-map'; @@ -96,7 +96,7 @@ describe('Bundle', function() { map: 'map', }; - require('uglify-js').minify = function() { + UglifyJS.minify = function() { return minified; }; @@ -246,35 +246,35 @@ describe('Bundle', function() { }); - function genSourceMap(modules) { - var sourceMapGen = new SourceMapGenerator({file: 'bundle.js', version: 3}); - var bundleLineNo = 0; - for (var i = 0; i < modules.length; i++) { - var module = modules[i]; - var transformedCode = module.code; - var sourcePath = module.sourcePath; - var sourceCode = module.sourceCode; - var transformedLineCount = 0; - var lastCharNewLine = false; - for (var t = 0; t < transformedCode.length; t++) { - if (t === 0 || lastCharNewLine) { - sourceMapGen.addMapping({ - generated: {line: bundleLineNo + 1, column: 0}, - original: {line: transformedLineCount + 1, column: 0}, - source: sourcePath - }); - } - lastCharNewLine = transformedCode[t] === '\n'; - if (lastCharNewLine) { - transformedLineCount++; - bundleLineNo++; - } - } - bundleLineNo++; - sourceMapGen.setSourceContent( - sourcePath, - sourceCode - ); - } - return sourceMapGen.toJSON(); - } +function genSourceMap(modules) { + var sourceMapGen = new SourceMapGenerator({file: 'bundle.js', version: 3}); + var bundleLineNo = 0; + for (var i = 0; i < modules.length; i++) { + var module = modules[i]; + var transformedCode = module.code; + var sourcePath = module.sourcePath; + var sourceCode = module.sourceCode; + var transformedLineCount = 0; + var lastCharNewLine = false; + for (var t = 0; t < transformedCode.length; t++) { + if (t === 0 || lastCharNewLine) { + sourceMapGen.addMapping({ + generated: {line: bundleLineNo + 1, column: 0}, + original: {line: transformedLineCount + 1, column: 0}, + source: sourcePath + }); + } + lastCharNewLine = transformedCode[t] === '\n'; + if (lastCharNewLine) { + transformedLineCount++; + bundleLineNo++; + } + } + bundleLineNo++; + sourceMapGen.setSourceContent( + sourcePath, + sourceCode + ); + } + return sourceMapGen.toJSON(); +} diff --git a/packager/react-packager/src/Bundler/__tests__/Bundler-test.js b/packager/react-packager/src/Bundler/__tests__/Bundler-test.js index fb205d8ac31668..b397f24e33dffc 100644 --- a/packager/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/packager/react-packager/src/Bundler/__tests__/Bundler-test.js @@ -17,41 +17,39 @@ jest jest.mock('fs'); -var Promise = require('promise'); +var Bundler = require('../'); +var JSTransformer = require('../../JSTransformer'); +var DependencyResolver = require('../../DependencyResolver'); +var sizeOf = require('image-size'); +var fs = require('fs'); describe('Bundler', function() { var getDependencies; var wrapModule; - var Bundler; var bundler; var assetServer; var modules; - var ProgressBar; beforeEach(function() { getDependencies = jest.genMockFn(); wrapModule = jest.genMockFn(); - require('../../DependencyResolver').mockImpl(function() { + DependencyResolver.mockImpl(function() { return { getDependencies: getDependencies, wrapModule: wrapModule, }; }); - Bundler = require('../'); - - require('fs').statSync.mockImpl(function() { + fs.statSync.mockImpl(function() { return { isDirectory: () => true }; }); - require('fs').readFile.mockImpl(function(file, callback) { + fs.readFile.mockImpl(function(file, callback) { callback(null, '{"json":true}'); }); - ProgressBar = require('progress'); - assetServer = { getAssetData: jest.genMockFn(), }; @@ -114,7 +112,7 @@ describe('Bundler', function() { }); }); - require('../../JSTransformer').prototype.loadFileAndTransform + JSTransformer.prototype.loadFileAndTransform .mockImpl(function(path) { return Promise.resolve({ code: 'transformed ' + path, @@ -128,7 +126,7 @@ describe('Bundler', function() { return Promise.resolve('lol ' + code + ' lol'); }); - require('image-size').mockImpl(function(path, cb) { + sizeOf.mockImpl(function(path, cb) { cb(null, { width: 50, height: 100 }); }); diff --git a/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js b/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js index 2f66d6ff6d8f73..99396bbc397b13 100644 --- a/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js +++ b/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayout-test.js @@ -11,19 +11,12 @@ jest.dontMock('../index'); jest.mock('fs'); -const Promise = require('promise'); +var Promise = require('promise'); +var BundlesLayout = require('../index'); +var DependencyResolver = require('../../DependencyResolver'); +var loadCacheSync = require('../../lib/loadCacheSync'); describe('BundlesLayout', () => { - let BundlesLayout; - let DependencyResolver; - let loadCacheSync; - - beforeEach(() => { - BundlesLayout = require('../index'); - DependencyResolver = require('../../DependencyResolver'); - loadCacheSync = require('../../lib/loadCacheSync'); - }); - function newBundlesLayout(options) { return new BundlesLayout(Object.assign({ projectRoots: ['/root'], diff --git a/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayoutIntegration-test.js b/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayoutIntegration-test.js index 7fea8f8f7d6e85..33fb2f9a3c5ea3 100644 --- a/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayoutIntegration-test.js +++ b/packager/react-packager/src/BundlesLayout/__tests__/BundlesLayoutIntegration-test.js @@ -17,12 +17,13 @@ const path = require('path'); jest.mock('fs'); +var BundlesLayout = require('../index'); +var Cache = require('../../Cache'); +var DependencyResolver = require('../../DependencyResolver'); +var fs = require('fs'); + describe('BundlesLayout', () => { - var BundlesLayout; - var Cache; - var DependencyResolver; var fileWatcher; - var fs; const polyfills = [ 'polyfills/prelude_dev.js', @@ -37,11 +38,6 @@ describe('BundlesLayout', () => { const baseFs = getBaseFs(); beforeEach(() => { - fs = require('fs'); - BundlesLayout = require('../index'); - Cache = require('../../Cache'); - DependencyResolver = require('../../DependencyResolver'); - fileWatcher = { on: () => this, isWatchman: () => Promise.resolve(false) diff --git a/packager/react-packager/src/Cache/__tests__/Cache-test.js b/packager/react-packager/src/Cache/__tests__/Cache-test.js index 8040e9e66e8f23..abb28b2e49be56 100644 --- a/packager/react-packager/src/Cache/__tests__/Cache-test.js +++ b/packager/react-packager/src/Cache/__tests__/Cache-test.js @@ -20,19 +20,19 @@ jest .mock('fs'); var Promise = require('promise'); +var Cache = require('../'); +var fs = require('fs'); +var os = require('os'); +var _ = require('underscore'); describe('JSTransformer Cache', () => { - var Cache; - beforeEach(() => { - require('os').tmpDir.mockImpl(() => 'tmpDir'); - - Cache = require('../'); + os.tmpDir.mockImpl(() => 'tmpDir'); }); describe('getting/setting', () => { pit('calls loader callback for uncached file', () => { - require('fs').stat.mockImpl((file, callback) => { + fs.stat.mockImpl((file, callback) => { callback(null, { mtime: { getTime: () => {} @@ -54,7 +54,7 @@ describe('JSTransformer Cache', () => { }); pit('supports storing multiple fields', () => { - require('fs').stat.mockImpl((file, callback) => { + fs.stat.mockImpl((file, callback) => { callback(null, { mtime: { getTime: () => {} @@ -82,7 +82,7 @@ describe('JSTransformer Cache', () => { }); pit('gets the value from the loader callback', () => { - require('fs').stat.mockImpl((file, callback) => + fs.stat.mockImpl((file, callback) => callback(null, { mtime: { getTime: () => {} @@ -104,7 +104,7 @@ describe('JSTransformer Cache', () => { }); pit('caches the value after the first call', () => { - require('fs').stat.mockImpl((file, callback) => { + fs.stat.mockImpl((file, callback) => { callback(null, { mtime: { getTime: () => {} @@ -134,7 +134,7 @@ describe('JSTransformer Cache', () => { pit('clears old field when getting new field and mtime changed', () => { var mtime = 0; - require('fs').stat.mockImpl((file, callback) => { + fs.stat.mockImpl((file, callback) => { callback(null, { mtime: { getTime: () => mtime++ @@ -179,8 +179,6 @@ describe('JSTransformer Cache', () => { } }; - var fs = require('fs'); - fs.existsSync.mockImpl(() => true); fs.statSync.mockImpl(filePath => fileStats[filePath]); @@ -220,7 +218,7 @@ describe('JSTransformer Cache', () => { }); pit('should not load outdated cache', () => { - require('fs').stat.mockImpl((file, callback) => + fs.stat.mockImpl((file, callback) => callback(null, { mtime: { getTime: () => {} @@ -259,7 +257,7 @@ describe('JSTransformer Cache', () => { var index = 0; var mtimes = [10, 20, 30]; var debounceIndex = 0; - require('underscore').debounce = callback => { + _.debounce = callback => { return () => { if (++debounceIndex === 3) { callback(); @@ -267,7 +265,6 @@ describe('JSTransformer Cache', () => { }; }; - var fs = require('fs'); fs.stat.mockImpl((file, callback) => callback(null, { mtime: { diff --git a/packager/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js b/packager/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js index c456ba29b5fe2d..4aa4031298f092 100644 --- a/packager/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/packager/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js @@ -16,12 +16,13 @@ jest .mock('fs') .mock('../../../Cache'); +var Cache = require('../../../Cache'); +var DependencyGraph = require('../index'); +var fs = require('fs'); + describe('DependencyGraph', function() { var cache; - var Cache; - var DependencyGraph; var fileWatcher; - var fs; function getOrderedDependenciesAsJSON(dgraph, entry, platform) { return dgraph.getDependencies(entry, platform) @@ -43,10 +44,6 @@ describe('DependencyGraph', function() { } beforeEach(function() { - fs = require('fs'); - Cache = require('../../../Cache'); - DependencyGraph = require('../index'); - fileWatcher = { on: function() { return this; diff --git a/packager/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js b/packager/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js index edb79cbb8ba423..5231dc4a530532 100644 --- a/packager/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js +++ b/packager/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js @@ -15,23 +15,21 @@ jest.dontMock('../') jest.mock('path'); var Promise = require('promise'); +var HasteDependencyResolver = require('../'); +var Module = require('../Module'); +var Polyfill = require('../Polyfill'); + +var path = require('path'); var _ = require('underscore'); describe('HasteDependencyResolver', function() { - var HasteDependencyResolver; - var Module; - var Polyfill; - beforeEach(function() { - Module = require('../Module'); - Polyfill = require('../Polyfill'); Polyfill.mockClear(); // For the polyfillDeps - require('path').join.mockImpl(function(a, b) { + path.join.mockImpl(function(a, b) { return b; }); - HasteDependencyResolver = require('../'); }); class ResolutionResponseMock { diff --git a/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js b/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js index 640157d94f38f1..21ca6288274a65 100644 --- a/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js +++ b/packager/react-packager/src/DependencyResolver/__tests__/Module-test.js @@ -19,24 +19,16 @@ jest jest .mock('fs'); -describe('Module', () => { - var Fastfs; - var Module; - var ModuleCache; - var Promise; - var fs; +var Fastfs = require('../fastfs'); +var Module = require('../Module'); +var ModuleCache = require('../ModuleCache'); +var Promise = require('promise'); +var fs = require('fs'); +var FileWatcher = require('../../FileWatcher'); - const FileWatcher = require('../../FileWatcher'); +describe('Module', () => { const fileWatcher = new FileWatcher(['/root']); - beforeEach(function() { - Fastfs = require('../fastfs'); - Module = require('../Module'); - ModuleCache = require('../ModuleCache'); - Promise = require('promise'); - fs = require('fs'); - }); - describe('Async Dependencies', () => { function expectAsyncDependenciesToEqual(expected) { var fastfs = new Fastfs( diff --git a/packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js b/packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js index a5eeda3da2de39..973c4f7804bc8f 100644 --- a/packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js +++ b/packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js @@ -13,19 +13,20 @@ jest .dontMock('events') .dontMock('../') .dontMock('q') - .setMock( - 'child_process', - { exec: function(cmd, cb) { cb(null, '/usr/bin/watchman'); } } - ); + .setMock('child_process', { + exec: function(cmd, cb) { + cb(null, '/usr/bin/watchman'); + } + }); + +var FileWatcher = require('../'); +var sane = require('sane'); describe('FileWatcher', function() { - var FileWatcher; var Watcher; beforeEach(function() { - require('mock-modules').dumpCache(); - FileWatcher = require('../'); - Watcher = require('sane').WatchmanWatcher; + Watcher = sane.WatchmanWatcher; Watcher.prototype.once.mockImplementation(function(type, callback) { callback(); }); @@ -65,5 +66,4 @@ describe('FileWatcher', function() { expect(Watcher.prototype.close).toBeCalled(); }); }); - }); diff --git a/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js b/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js index fdcd17eba26951..b182e1eb2aa85d 100644 --- a/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js +++ b/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js @@ -9,21 +9,18 @@ 'use strict'; jest - .dontMock('worker-farm') .dontMock('../../lib/ModuleTransport') - .dontMock('../index'); + .dontMock('../'); jest.mock('fs'); var Cache = require('../../Cache'); +var Transformer = require('../'); +var fs = require('fs'); -var OPTIONS = { - transformModulePath: '/foo/bar', - cache: new Cache({}), -}; +var options; describe('Transformer', function() { - var Transformer; var workers; beforeEach(function() { @@ -31,21 +28,22 @@ describe('Transformer', function() { jest.setMock('worker-farm', jest.genMockFn().mockImpl(function() { return workers; })); - require('fs').readFile.mockImpl(function(file, callback) { - callback(null, 'content'); - }); - Transformer = require('../'); + + options = { + transformModulePath: '/foo/bar', + cache: new Cache({}), + }; }); pit('should loadFileAndTransform', function() { workers.mockImpl(function(data, callback) { callback(null, { code: 'transformed', map: 'sourceMap' }); }); - require('fs').readFile.mockImpl(function(file, callback) { + fs.readFile.mockImpl(function(file, callback) { callback(null, 'content'); }); - return new Transformer(OPTIONS).loadFileAndTransform('file') + return new Transformer(options).loadFileAndTransform('file') .then(function(data) { expect(data).toEqual({ code: 'transformed', @@ -60,7 +58,7 @@ describe('Transformer', function() { var message = 'message'; var snippet = 'snippet'; - require('fs').readFile.mockImpl(function(file, callback) { + fs.readFile.mockImpl(function(file, callback) { callback(null, 'var x;\nvar answer = 1 = x;'); }); @@ -76,7 +74,7 @@ describe('Transformer', function() { callback(babelError); }); - return new Transformer(OPTIONS).loadFileAndTransform('foo-file.js') + return new Transformer(options).loadFileAndTransform('foo-file.js') .catch(function(error) { expect(error.type).toEqual('TransformError'); expect(error.message).toBe('SyntaxError ' + message); diff --git a/packager/react-packager/src/Server/__tests__/Server-test.js b/packager/react-packager/src/Server/__tests__/Server-test.js index be4feebfd54c94..6da2b195f29a83 100644 --- a/packager/react-packager/src/Server/__tests__/Server-test.js +++ b/packager/react-packager/src/Server/__tests__/Server-test.js @@ -18,10 +18,14 @@ jest.setMock('worker-farm', function() { return () => {}; }) const Promise = require('promise'); +var Bundler = require('../../Bundler'); +var FileWatcher = require('../../FileWatcher'); +var Server = require('../'); +var Server = require('../../Server'); +var AssetServer = require('../../AssetServer'); + describe('processRequest', () => { var server; - var Bundler; - var FileWatcher; const options = { projectRoots: ['root'], @@ -47,9 +51,6 @@ describe('processRequest', () => { var triggerFileChange; beforeEach(() => { - Bundler = require('../../Bundler'); - FileWatcher = require('../../FileWatcher'); - Bundler.prototype.bundle = jest.genMockFunction().mockImpl(() => Promise.resolve({ getSource: () => 'this is the source', @@ -68,7 +69,6 @@ describe('processRequest', () => { Bundler.prototype.invalidateFile = invalidatorFunc; - const Server = require('../'); server = new Server(options); requestHandler = server.processRequest.bind(server); }); @@ -172,7 +172,6 @@ describe('processRequest', () => { Bundler.prototype.bundle = bundleFunc; - const Server = require('../../Server'); server = new Server(options); requestHandler = server.processRequest.bind(server); @@ -232,11 +231,6 @@ describe('processRequest', () => { }); describe('/assets endpoint', () => { - var AssetServer; - beforeEach(() => { - AssetServer = require('../../AssetServer'); - }); - it('should serve simple case', () => { const req = {url: '/assets/imgs/a.png'}; const res = {end: jest.genMockFn()}; diff --git a/packager/react-packager/src/SocketInterface/__tests__/SocketClient-test.js b/packager/react-packager/src/SocketInterface/__tests__/SocketClient-test.js index 59477fcf1764ce..df8d8f0960fe86 100644 --- a/packager/react-packager/src/SocketInterface/__tests__/SocketClient-test.js +++ b/packager/react-packager/src/SocketInterface/__tests__/SocketClient-test.js @@ -13,26 +13,27 @@ jest.setMock('worker-farm', function() { return () => {}; }) .mock('net') .dontMock('../SocketClient'); +var Bundle = require('../../Bundler/Bundle'); +var SocketClient = require('../SocketClient'); +var bser = require('bser'); +var net = require('net'); + describe('SocketClient', () => { - let SocketClient; let sock; let bunser; beforeEach(() => { - SocketClient = require('../SocketClient'); - const {EventEmitter} = require.requireActual('events'); sock = new EventEmitter(); sock.write = jest.genMockFn(); - require('net').connect.mockImpl(() => sock); + net.connect.mockImpl(() => sock); - const bser = require('bser'); bunser = new EventEmitter(); - require('bser').BunserBuf.mockImpl(() => bunser); + bser.BunserBuf.mockImpl(() => bunser); bser.dumpToBuffer.mockImpl((a) => a); - require('../../Bundler/Bundle').fromJSON.mockImpl((a) => a); + Bundle.fromJSON.mockImpl((a) => a); }); pit('create a connection', () => { @@ -40,7 +41,7 @@ describe('SocketClient', () => { sock.emit('connect'); return client.onReady().then(c => { expect(c).toBe(client); - expect(require('net').connect).toBeCalledWith('/sock'); + expect(net.connect).toBeCalledWith('/sock'); }); }); diff --git a/packager/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js b/packager/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js index d43d35861c869b..729d2a1ddfb20a 100644 --- a/packager/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js +++ b/packager/react-packager/src/SocketInterface/__tests__/SocketInterface-test.js @@ -13,23 +13,19 @@ jest.setMock('worker-farm', function() { return () => {}; }) .mock('child_process') .dontMock('../'); -describe('SocketInterface', () => { - let SocketInterface; - let SocketClient; - - beforeEach(() => { - SocketInterface = require('../'); - SocketClient = require('../SocketClient'); - }); +var SocketInterface = require('../'); +var SocketClient = require('../SocketClient'); +var childProcess = require('child_process'); +var fs = require('fs'); +describe('SocketInterface', () => { describe('getOrCreateSocketFor', () => { pit('creates socket path by hashing options', () => { - const fs = require('fs'); fs.existsSync = jest.genMockFn().mockImpl(() => true); fs.unlinkSync = jest.genMockFn(); let callback; - require('child_process').spawn.mockImpl(() => ({ + childProcess.spawn.mockImpl(() => ({ on: (event, cb) => callback = cb, send: (message) => { setImmediate(() => callback({ type: 'createdServer' })); @@ -58,13 +54,12 @@ describe('SocketInterface', () => { }); pit('should fork a server', () => { - const fs = require('fs'); fs.existsSync = jest.genMockFn().mockImpl(() => false); fs.unlinkSync = jest.genMockFn(); let sockPath; let callback; - require('child_process').spawn.mockImpl(() => ({ + childProcess.spawn.mockImpl(() => ({ on: (event, cb) => callback = cb, send: (message) => { expect(message.type).toBe('createSocketServer'); diff --git a/packager/react-packager/src/SocketInterface/__tests__/SocketServer-test.js b/packager/react-packager/src/SocketInterface/__tests__/SocketServer-test.js index ae3e8e2489b1c5..e006d61252e2b6 100644 --- a/packager/react-packager/src/SocketInterface/__tests__/SocketServer-test.js +++ b/packager/react-packager/src/SocketInterface/__tests__/SocketServer-test.js @@ -14,26 +14,24 @@ jest.setMock('worker-farm', function() { return () => {}; }) .mock('fs') .dontMock('../SocketServer'); +var PackagerServer = require('../../Server'); +var SocketServer = require('../SocketServer'); +var bser = require('bser'); +var net = require('net'); + describe('SocketServer', () => { - let PackagerServer; - let SocketServer; let netServer; let bunser; beforeEach(() => { - SocketServer = require('../SocketServer'); - const {EventEmitter} = require.requireActual('events'); netServer = new EventEmitter(); netServer.listen = jest.genMockFn(); - require('net').createServer.mockImpl(() => netServer); + net.createServer.mockImpl(() => netServer); - const bser = require('bser'); bunser = new EventEmitter(); bser.BunserBuf.mockImpl(() => bunser); bser.dumpToBuffer.mockImpl((a) => a); - - PackagerServer = require('../../Server'); }); pit('create a server', () => { diff --git a/packager/react-packager/src/lib/__tests__/getAssetDataFromName-test.js b/packager/react-packager/src/lib/__tests__/getAssetDataFromName-test.js index d671100151fbc3..ff61c40537d419 100644 --- a/packager/react-packager/src/lib/__tests__/getAssetDataFromName-test.js +++ b/packager/react-packager/src/lib/__tests__/getAssetDataFromName-test.js @@ -11,13 +11,9 @@ jest.dontMock('../getPlatformExtension') .dontMock('../getAssetDataFromName'); -describe('getAssetDataFromName', () => { - let getAssetDataFromName; - - beforeEach(() => { - getAssetDataFromName = require('../getAssetDataFromName'); - }); +var getAssetDataFromName = require('../getAssetDataFromName'); +describe('getAssetDataFromName', () => { it('should get data from name', () => { expect(getAssetDataFromName('a/b/c.png')).toEqual({ resolution: 1, diff --git a/packager/react-packager/src/lib/__tests__/getPotentialPlatformExt-test.js b/packager/react-packager/src/lib/__tests__/getPotentialPlatformExt-test.js index 5f734880e56bb6..f5ccdc3ab9b4f2 100644 --- a/packager/react-packager/src/lib/__tests__/getPotentialPlatformExt-test.js +++ b/packager/react-packager/src/lib/__tests__/getPotentialPlatformExt-test.js @@ -10,9 +10,10 @@ jest.dontMock('../getPlatformExtension'); +var getPlatformExtension = require('../getPlatformExtension'); + describe('getPlatformExtension', function() { it('should get platform ext', function() { - var getPlatformExtension = require('../getPlatformExtension'); expect(getPlatformExtension('a.ios.js')).toBe('ios'); expect(getPlatformExtension('a.android.js')).toBe('android'); expect(getPlatformExtension('/b/c/a.ios.js')).toBe('ios');