Skip to content

Commit

Permalink
Codemod tests to use top-level-requires
Browse files Browse the repository at this point in the history
Reviewed By: @DmitrySoshnikov

Differential Revision: D2456250
  • Loading branch information
cpojer authored and facebook-github-bot-5 committed Sep 19, 2015
1 parent 3fca7f4 commit fa01b2e
Show file tree
Hide file tree
Showing 22 changed files with 162 additions and 220 deletions.
7 changes: 4 additions & 3 deletions Libraries/Animated/__tests__/Animated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 10 additions & 11 deletions Libraries/Image/__tests__/resolveAssetSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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';
});

Expand Down Expand Up @@ -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';
});
Expand All @@ -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';
});
Expand Down
3 changes: 0 additions & 3 deletions jestSupport/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@
'use strict';

global.__DEV__ = true;
global.setImmediate = global.setImmediate || function(fn) {
return setTimeout(fn, 0);
};
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"scriptPreprocessor": "jestSupport/preprocessor.js",
"setupEnvScriptFile": "jestSupport/env.js",
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
"/node_modules/"
],
"testFileExtensions": [
"js"
Expand All @@ -38,7 +37,7 @@
"jestSupport"
],
"scripts": {
"test": "jest",
"test": "NODE_ENV=test jest",
"lint": "node linter.js Examples/ Libraries/",
"start": "./packager/packager.sh || true"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -68,9 +68,7 @@ describe('Activity', () => {

expect(() => {
Activity.endEvent(eid);
}).toThrow('event(3) has already ended!');

jest.runOnlyPendingTimers();
}).toThrow('event(1) has already ended!');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
74 changes: 37 additions & 37 deletions packager/react-packager/src/Bundler/__tests__/Bundle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('Bundle', function() {
map: 'map',
};

require('uglify-js').minify = function() {
UglifyJS.minify = function() {
return minified;
};

Expand Down Expand Up @@ -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();
}
22 changes: 10 additions & 12 deletions packager/react-packager/src/Bundler/__tests__/Bundler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
Expand Down Expand Up @@ -114,7 +112,7 @@ describe('Bundler', function() {
});
});

require('../../JSTransformer').prototype.loadFileAndTransform
JSTransformer.prototype.loadFileAndTransform
.mockImpl(function(path) {
return Promise.resolve({
code: 'transformed ' + path,
Expand All @@ -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 });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
Expand Down
Loading

0 comments on commit fa01b2e

Please sign in to comment.