mirror of https://github.com/status-im/metro.git
Codemod tests to use top-level-requires
Reviewed By: @DmitrySoshnikov Differential Revision: D2456250
This commit is contained in:
parent
358b49597b
commit
3f02fd9371
|
@ -10,13 +10,13 @@
|
||||||
|
|
||||||
jest.autoMockOff();
|
jest.autoMockOff();
|
||||||
|
|
||||||
|
var Activity = require('../');
|
||||||
|
|
||||||
describe('Activity', () => {
|
describe('Activity', () => {
|
||||||
const origConsoleLog = console.log;
|
const origConsoleLog = console.log;
|
||||||
let Activity;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
console.log = jest.genMockFn();
|
console.log = jest.genMockFn();
|
||||||
Activity = require('../');
|
|
||||||
jest.runOnlyPendingTimers();
|
jest.runOnlyPendingTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -68,9 +68,7 @@ describe('Activity', () => {
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Activity.endEvent(eid);
|
Activity.endEvent(eid);
|
||||||
}).toThrow('event(3) has already ended!');
|
}).toThrow('event(1) has already ended!');
|
||||||
|
|
||||||
jest.runOnlyPendingTimers();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,17 +11,11 @@ jest
|
||||||
|
|
||||||
const Promise = require('promise');
|
const Promise = require('promise');
|
||||||
|
|
||||||
|
var AssetServer = require('../');
|
||||||
|
var crypto = require('crypto');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
describe('AssetServer', () => {
|
describe('AssetServer', () => {
|
||||||
let AssetServer;
|
|
||||||
let crypto;
|
|
||||||
let fs;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
AssetServer = require('../');
|
|
||||||
crypto = require('crypto');
|
|
||||||
fs = require('fs');
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('assetServer.get', () => {
|
describe('assetServer.get', () => {
|
||||||
pit('should work for the simple case', () => {
|
pit('should work for the simple case', () => {
|
||||||
const server = new AssetServer({
|
const server = new AssetServer({
|
||||||
|
|
|
@ -12,14 +12,14 @@ jest.autoMockOff();
|
||||||
|
|
||||||
var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
||||||
|
|
||||||
|
var Bundle = require('../Bundle');
|
||||||
|
var ModuleTransport = require('../../lib/ModuleTransport');
|
||||||
|
var UglifyJS = require('uglify-js');
|
||||||
|
|
||||||
describe('Bundle', function() {
|
describe('Bundle', function() {
|
||||||
var ModuleTransport;
|
|
||||||
var Bundle;
|
|
||||||
var bundle;
|
var bundle;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
Bundle = require('../Bundle');
|
|
||||||
ModuleTransport = require('../../lib/ModuleTransport');
|
|
||||||
bundle = new Bundle('test_url');
|
bundle = new Bundle('test_url');
|
||||||
bundle.getSourceMap = jest.genMockFn().mockImpl(function() {
|
bundle.getSourceMap = jest.genMockFn().mockImpl(function() {
|
||||||
return 'test-source-map';
|
return 'test-source-map';
|
||||||
|
@ -96,7 +96,7 @@ describe('Bundle', function() {
|
||||||
map: 'map',
|
map: 'map',
|
||||||
};
|
};
|
||||||
|
|
||||||
require('uglify-js').minify = function() {
|
UglifyJS.minify = function() {
|
||||||
return minified;
|
return minified;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -246,35 +246,35 @@ describe('Bundle', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function genSourceMap(modules) {
|
function genSourceMap(modules) {
|
||||||
var sourceMapGen = new SourceMapGenerator({file: 'bundle.js', version: 3});
|
var sourceMapGen = new SourceMapGenerator({file: 'bundle.js', version: 3});
|
||||||
var bundleLineNo = 0;
|
var bundleLineNo = 0;
|
||||||
for (var i = 0; i < modules.length; i++) {
|
for (var i = 0; i < modules.length; i++) {
|
||||||
var module = modules[i];
|
var module = modules[i];
|
||||||
var transformedCode = module.code;
|
var transformedCode = module.code;
|
||||||
var sourcePath = module.sourcePath;
|
var sourcePath = module.sourcePath;
|
||||||
var sourceCode = module.sourceCode;
|
var sourceCode = module.sourceCode;
|
||||||
var transformedLineCount = 0;
|
var transformedLineCount = 0;
|
||||||
var lastCharNewLine = false;
|
var lastCharNewLine = false;
|
||||||
for (var t = 0; t < transformedCode.length; t++) {
|
for (var t = 0; t < transformedCode.length; t++) {
|
||||||
if (t === 0 || lastCharNewLine) {
|
if (t === 0 || lastCharNewLine) {
|
||||||
sourceMapGen.addMapping({
|
sourceMapGen.addMapping({
|
||||||
generated: {line: bundleLineNo + 1, column: 0},
|
generated: {line: bundleLineNo + 1, column: 0},
|
||||||
original: {line: transformedLineCount + 1, column: 0},
|
original: {line: transformedLineCount + 1, column: 0},
|
||||||
source: sourcePath
|
source: sourcePath
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
lastCharNewLine = transformedCode[t] === '\n';
|
lastCharNewLine = transformedCode[t] === '\n';
|
||||||
if (lastCharNewLine) {
|
if (lastCharNewLine) {
|
||||||
transformedLineCount++;
|
transformedLineCount++;
|
||||||
bundleLineNo++;
|
bundleLineNo++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bundleLineNo++;
|
bundleLineNo++;
|
||||||
sourceMapGen.setSourceContent(
|
sourceMapGen.setSourceContent(
|
||||||
sourcePath,
|
sourcePath,
|
||||||
sourceCode
|
sourceCode
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return sourceMapGen.toJSON();
|
return sourceMapGen.toJSON();
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,41 +17,39 @@ jest
|
||||||
|
|
||||||
jest.mock('fs');
|
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() {
|
describe('Bundler', function() {
|
||||||
var getDependencies;
|
var getDependencies;
|
||||||
var wrapModule;
|
var wrapModule;
|
||||||
var Bundler;
|
|
||||||
var bundler;
|
var bundler;
|
||||||
var assetServer;
|
var assetServer;
|
||||||
var modules;
|
var modules;
|
||||||
var ProgressBar;
|
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
getDependencies = jest.genMockFn();
|
getDependencies = jest.genMockFn();
|
||||||
wrapModule = jest.genMockFn();
|
wrapModule = jest.genMockFn();
|
||||||
require('../../DependencyResolver').mockImpl(function() {
|
DependencyResolver.mockImpl(function() {
|
||||||
return {
|
return {
|
||||||
getDependencies: getDependencies,
|
getDependencies: getDependencies,
|
||||||
wrapModule: wrapModule,
|
wrapModule: wrapModule,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
Bundler = require('../');
|
fs.statSync.mockImpl(function() {
|
||||||
|
|
||||||
require('fs').statSync.mockImpl(function() {
|
|
||||||
return {
|
return {
|
||||||
isDirectory: () => true
|
isDirectory: () => true
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
require('fs').readFile.mockImpl(function(file, callback) {
|
fs.readFile.mockImpl(function(file, callback) {
|
||||||
callback(null, '{"json":true}');
|
callback(null, '{"json":true}');
|
||||||
});
|
});
|
||||||
|
|
||||||
ProgressBar = require('progress');
|
|
||||||
|
|
||||||
assetServer = {
|
assetServer = {
|
||||||
getAssetData: jest.genMockFn(),
|
getAssetData: jest.genMockFn(),
|
||||||
};
|
};
|
||||||
|
@ -114,7 +112,7 @@ describe('Bundler', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
require('../../JSTransformer').prototype.loadFileAndTransform
|
JSTransformer.prototype.loadFileAndTransform
|
||||||
.mockImpl(function(path) {
|
.mockImpl(function(path) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
code: 'transformed ' + path,
|
code: 'transformed ' + path,
|
||||||
|
@ -128,7 +126,7 @@ describe('Bundler', function() {
|
||||||
return Promise.resolve('lol ' + code + ' lol');
|
return Promise.resolve('lol ' + code + ' lol');
|
||||||
});
|
});
|
||||||
|
|
||||||
require('image-size').mockImpl(function(path, cb) {
|
sizeOf.mockImpl(function(path, cb) {
|
||||||
cb(null, { width: 50, height: 100 });
|
cb(null, { width: 50, height: 100 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,19 +11,12 @@
|
||||||
jest.dontMock('../index');
|
jest.dontMock('../index');
|
||||||
jest.mock('fs');
|
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', () => {
|
describe('BundlesLayout', () => {
|
||||||
let BundlesLayout;
|
|
||||||
let DependencyResolver;
|
|
||||||
let loadCacheSync;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
BundlesLayout = require('../index');
|
|
||||||
DependencyResolver = require('../../DependencyResolver');
|
|
||||||
loadCacheSync = require('../../lib/loadCacheSync');
|
|
||||||
});
|
|
||||||
|
|
||||||
function newBundlesLayout(options) {
|
function newBundlesLayout(options) {
|
||||||
return new BundlesLayout(Object.assign({
|
return new BundlesLayout(Object.assign({
|
||||||
projectRoots: ['/root'],
|
projectRoots: ['/root'],
|
||||||
|
|
|
@ -17,12 +17,13 @@ const path = require('path');
|
||||||
|
|
||||||
jest.mock('fs');
|
jest.mock('fs');
|
||||||
|
|
||||||
|
var BundlesLayout = require('../index');
|
||||||
|
var Cache = require('../../Cache');
|
||||||
|
var DependencyResolver = require('../../DependencyResolver');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
describe('BundlesLayout', () => {
|
describe('BundlesLayout', () => {
|
||||||
var BundlesLayout;
|
|
||||||
var Cache;
|
|
||||||
var DependencyResolver;
|
|
||||||
var fileWatcher;
|
var fileWatcher;
|
||||||
var fs;
|
|
||||||
|
|
||||||
const polyfills = [
|
const polyfills = [
|
||||||
'polyfills/prelude_dev.js',
|
'polyfills/prelude_dev.js',
|
||||||
|
@ -37,11 +38,6 @@ describe('BundlesLayout', () => {
|
||||||
const baseFs = getBaseFs();
|
const baseFs = getBaseFs();
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fs = require('fs');
|
|
||||||
BundlesLayout = require('../index');
|
|
||||||
Cache = require('../../Cache');
|
|
||||||
DependencyResolver = require('../../DependencyResolver');
|
|
||||||
|
|
||||||
fileWatcher = {
|
fileWatcher = {
|
||||||
on: () => this,
|
on: () => this,
|
||||||
isWatchman: () => Promise.resolve(false)
|
isWatchman: () => Promise.resolve(false)
|
||||||
|
|
|
@ -20,19 +20,19 @@ jest
|
||||||
.mock('fs');
|
.mock('fs');
|
||||||
|
|
||||||
var Promise = require('promise');
|
var Promise = require('promise');
|
||||||
|
var Cache = require('../');
|
||||||
|
var fs = require('fs');
|
||||||
|
var os = require('os');
|
||||||
|
var _ = require('underscore');
|
||||||
|
|
||||||
describe('JSTransformer Cache', () => {
|
describe('JSTransformer Cache', () => {
|
||||||
var Cache;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
require('os').tmpDir.mockImpl(() => 'tmpDir');
|
os.tmpDir.mockImpl(() => 'tmpDir');
|
||||||
|
|
||||||
Cache = require('../');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getting/setting', () => {
|
describe('getting/setting', () => {
|
||||||
pit('calls loader callback for uncached file', () => {
|
pit('calls loader callback for uncached file', () => {
|
||||||
require('fs').stat.mockImpl((file, callback) => {
|
fs.stat.mockImpl((file, callback) => {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
getTime: () => {}
|
getTime: () => {}
|
||||||
|
@ -54,7 +54,7 @@ describe('JSTransformer Cache', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('supports storing multiple fields', () => {
|
pit('supports storing multiple fields', () => {
|
||||||
require('fs').stat.mockImpl((file, callback) => {
|
fs.stat.mockImpl((file, callback) => {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
getTime: () => {}
|
getTime: () => {}
|
||||||
|
@ -82,7 +82,7 @@ describe('JSTransformer Cache', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('gets the value from the loader callback', () => {
|
pit('gets the value from the loader callback', () => {
|
||||||
require('fs').stat.mockImpl((file, callback) =>
|
fs.stat.mockImpl((file, callback) =>
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
getTime: () => {}
|
getTime: () => {}
|
||||||
|
@ -104,7 +104,7 @@ describe('JSTransformer Cache', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('caches the value after the first call', () => {
|
pit('caches the value after the first call', () => {
|
||||||
require('fs').stat.mockImpl((file, callback) => {
|
fs.stat.mockImpl((file, callback) => {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
getTime: () => {}
|
getTime: () => {}
|
||||||
|
@ -134,7 +134,7 @@ describe('JSTransformer Cache', () => {
|
||||||
|
|
||||||
pit('clears old field when getting new field and mtime changed', () => {
|
pit('clears old field when getting new field and mtime changed', () => {
|
||||||
var mtime = 0;
|
var mtime = 0;
|
||||||
require('fs').stat.mockImpl((file, callback) => {
|
fs.stat.mockImpl((file, callback) => {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
getTime: () => mtime++
|
getTime: () => mtime++
|
||||||
|
@ -179,8 +179,6 @@ describe('JSTransformer Cache', () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var fs = require('fs');
|
|
||||||
|
|
||||||
fs.existsSync.mockImpl(() => true);
|
fs.existsSync.mockImpl(() => true);
|
||||||
|
|
||||||
fs.statSync.mockImpl(filePath => fileStats[filePath]);
|
fs.statSync.mockImpl(filePath => fileStats[filePath]);
|
||||||
|
@ -220,7 +218,7 @@ describe('JSTransformer Cache', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('should not load outdated cache', () => {
|
pit('should not load outdated cache', () => {
|
||||||
require('fs').stat.mockImpl((file, callback) =>
|
fs.stat.mockImpl((file, callback) =>
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
getTime: () => {}
|
getTime: () => {}
|
||||||
|
@ -259,7 +257,7 @@ describe('JSTransformer Cache', () => {
|
||||||
var index = 0;
|
var index = 0;
|
||||||
var mtimes = [10, 20, 30];
|
var mtimes = [10, 20, 30];
|
||||||
var debounceIndex = 0;
|
var debounceIndex = 0;
|
||||||
require('underscore').debounce = callback => {
|
_.debounce = callback => {
|
||||||
return () => {
|
return () => {
|
||||||
if (++debounceIndex === 3) {
|
if (++debounceIndex === 3) {
|
||||||
callback();
|
callback();
|
||||||
|
@ -267,7 +265,6 @@ describe('JSTransformer Cache', () => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
var fs = require('fs');
|
|
||||||
fs.stat.mockImpl((file, callback) =>
|
fs.stat.mockImpl((file, callback) =>
|
||||||
callback(null, {
|
callback(null, {
|
||||||
mtime: {
|
mtime: {
|
||||||
|
|
|
@ -16,12 +16,13 @@ jest
|
||||||
.mock('fs')
|
.mock('fs')
|
||||||
.mock('../../../Cache');
|
.mock('../../../Cache');
|
||||||
|
|
||||||
|
var Cache = require('../../../Cache');
|
||||||
|
var DependencyGraph = require('../index');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
describe('DependencyGraph', function() {
|
describe('DependencyGraph', function() {
|
||||||
var cache;
|
var cache;
|
||||||
var Cache;
|
|
||||||
var DependencyGraph;
|
|
||||||
var fileWatcher;
|
var fileWatcher;
|
||||||
var fs;
|
|
||||||
|
|
||||||
function getOrderedDependenciesAsJSON(dgraph, entry, platform) {
|
function getOrderedDependenciesAsJSON(dgraph, entry, platform) {
|
||||||
return dgraph.getDependencies(entry, platform)
|
return dgraph.getDependencies(entry, platform)
|
||||||
|
@ -43,10 +44,6 @@ describe('DependencyGraph', function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
fs = require('fs');
|
|
||||||
Cache = require('../../../Cache');
|
|
||||||
DependencyGraph = require('../index');
|
|
||||||
|
|
||||||
fileWatcher = {
|
fileWatcher = {
|
||||||
on: function() {
|
on: function() {
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -15,23 +15,21 @@ jest.dontMock('../')
|
||||||
jest.mock('path');
|
jest.mock('path');
|
||||||
|
|
||||||
var Promise = require('promise');
|
var Promise = require('promise');
|
||||||
|
var HasteDependencyResolver = require('../');
|
||||||
|
var Module = require('../Module');
|
||||||
|
var Polyfill = require('../Polyfill');
|
||||||
|
|
||||||
|
var path = require('path');
|
||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
|
|
||||||
describe('HasteDependencyResolver', function() {
|
describe('HasteDependencyResolver', function() {
|
||||||
var HasteDependencyResolver;
|
|
||||||
var Module;
|
|
||||||
var Polyfill;
|
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
Module = require('../Module');
|
|
||||||
Polyfill = require('../Polyfill');
|
|
||||||
Polyfill.mockClear();
|
Polyfill.mockClear();
|
||||||
|
|
||||||
// For the polyfillDeps
|
// For the polyfillDeps
|
||||||
require('path').join.mockImpl(function(a, b) {
|
path.join.mockImpl(function(a, b) {
|
||||||
return b;
|
return b;
|
||||||
});
|
});
|
||||||
HasteDependencyResolver = require('../');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
class ResolutionResponseMock {
|
class ResolutionResponseMock {
|
||||||
|
|
|
@ -19,24 +19,16 @@ jest
|
||||||
jest
|
jest
|
||||||
.mock('fs');
|
.mock('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');
|
||||||
|
|
||||||
describe('Module', () => {
|
describe('Module', () => {
|
||||||
var Fastfs;
|
|
||||||
var Module;
|
|
||||||
var ModuleCache;
|
|
||||||
var Promise;
|
|
||||||
var fs;
|
|
||||||
|
|
||||||
const FileWatcher = require('../../FileWatcher');
|
|
||||||
const fileWatcher = new FileWatcher(['/root']);
|
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', () => {
|
describe('Async Dependencies', () => {
|
||||||
function expectAsyncDependenciesToEqual(expected) {
|
function expectAsyncDependenciesToEqual(expected) {
|
||||||
var fastfs = new Fastfs(
|
var fastfs = new Fastfs(
|
||||||
|
|
|
@ -13,19 +13,20 @@ jest
|
||||||
.dontMock('events')
|
.dontMock('events')
|
||||||
.dontMock('../')
|
.dontMock('../')
|
||||||
.dontMock('q')
|
.dontMock('q')
|
||||||
.setMock(
|
.setMock('child_process', {
|
||||||
'child_process',
|
exec: function(cmd, cb) {
|
||||||
{ exec: function(cmd, cb) { cb(null, '/usr/bin/watchman'); } }
|
cb(null, '/usr/bin/watchman');
|
||||||
);
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var FileWatcher = require('../');
|
||||||
|
var sane = require('sane');
|
||||||
|
|
||||||
describe('FileWatcher', function() {
|
describe('FileWatcher', function() {
|
||||||
var FileWatcher;
|
|
||||||
var Watcher;
|
var Watcher;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
require('mock-modules').dumpCache();
|
Watcher = sane.WatchmanWatcher;
|
||||||
FileWatcher = require('../');
|
|
||||||
Watcher = require('sane').WatchmanWatcher;
|
|
||||||
Watcher.prototype.once.mockImplementation(function(type, callback) {
|
Watcher.prototype.once.mockImplementation(function(type, callback) {
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
|
@ -65,5 +66,4 @@ describe('FileWatcher', function() {
|
||||||
expect(Watcher.prototype.close).toBeCalled();
|
expect(Watcher.prototype.close).toBeCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,21 +9,18 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
jest
|
jest
|
||||||
.dontMock('worker-farm')
|
|
||||||
.dontMock('../../lib/ModuleTransport')
|
.dontMock('../../lib/ModuleTransport')
|
||||||
.dontMock('../index');
|
.dontMock('../');
|
||||||
|
|
||||||
jest.mock('fs');
|
jest.mock('fs');
|
||||||
|
|
||||||
var Cache = require('../../Cache');
|
var Cache = require('../../Cache');
|
||||||
|
var Transformer = require('../');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
var OPTIONS = {
|
var options;
|
||||||
transformModulePath: '/foo/bar',
|
|
||||||
cache: new Cache({}),
|
|
||||||
};
|
|
||||||
|
|
||||||
describe('Transformer', function() {
|
describe('Transformer', function() {
|
||||||
var Transformer;
|
|
||||||
var workers;
|
var workers;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
@ -31,21 +28,22 @@ describe('Transformer', function() {
|
||||||
jest.setMock('worker-farm', jest.genMockFn().mockImpl(function() {
|
jest.setMock('worker-farm', jest.genMockFn().mockImpl(function() {
|
||||||
return workers;
|
return workers;
|
||||||
}));
|
}));
|
||||||
require('fs').readFile.mockImpl(function(file, callback) {
|
|
||||||
callback(null, 'content');
|
options = {
|
||||||
});
|
transformModulePath: '/foo/bar',
|
||||||
Transformer = require('../');
|
cache: new Cache({}),
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('should loadFileAndTransform', function() {
|
pit('should loadFileAndTransform', function() {
|
||||||
workers.mockImpl(function(data, callback) {
|
workers.mockImpl(function(data, callback) {
|
||||||
callback(null, { code: 'transformed', map: 'sourceMap' });
|
callback(null, { code: 'transformed', map: 'sourceMap' });
|
||||||
});
|
});
|
||||||
require('fs').readFile.mockImpl(function(file, callback) {
|
fs.readFile.mockImpl(function(file, callback) {
|
||||||
callback(null, 'content');
|
callback(null, 'content');
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Transformer(OPTIONS).loadFileAndTransform('file')
|
return new Transformer(options).loadFileAndTransform('file')
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
expect(data).toEqual({
|
expect(data).toEqual({
|
||||||
code: 'transformed',
|
code: 'transformed',
|
||||||
|
@ -60,7 +58,7 @@ describe('Transformer', function() {
|
||||||
var message = 'message';
|
var message = 'message';
|
||||||
var snippet = 'snippet';
|
var snippet = 'snippet';
|
||||||
|
|
||||||
require('fs').readFile.mockImpl(function(file, callback) {
|
fs.readFile.mockImpl(function(file, callback) {
|
||||||
callback(null, 'var x;\nvar answer = 1 = x;');
|
callback(null, 'var x;\nvar answer = 1 = x;');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -76,7 +74,7 @@ describe('Transformer', function() {
|
||||||
callback(babelError);
|
callback(babelError);
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Transformer(OPTIONS).loadFileAndTransform('foo-file.js')
|
return new Transformer(options).loadFileAndTransform('foo-file.js')
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
expect(error.type).toEqual('TransformError');
|
expect(error.type).toEqual('TransformError');
|
||||||
expect(error.message).toBe('SyntaxError ' + message);
|
expect(error.message).toBe('SyntaxError ' + message);
|
||||||
|
|
|
@ -18,10 +18,14 @@ jest.setMock('worker-farm', function() { return () => {}; })
|
||||||
|
|
||||||
const Promise = require('promise');
|
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', () => {
|
describe('processRequest', () => {
|
||||||
var server;
|
var server;
|
||||||
var Bundler;
|
|
||||||
var FileWatcher;
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
projectRoots: ['root'],
|
projectRoots: ['root'],
|
||||||
|
@ -47,9 +51,6 @@ describe('processRequest', () => {
|
||||||
var triggerFileChange;
|
var triggerFileChange;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
Bundler = require('../../Bundler');
|
|
||||||
FileWatcher = require('../../FileWatcher');
|
|
||||||
|
|
||||||
Bundler.prototype.bundle = jest.genMockFunction().mockImpl(() =>
|
Bundler.prototype.bundle = jest.genMockFunction().mockImpl(() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
getSource: () => 'this is the source',
|
getSource: () => 'this is the source',
|
||||||
|
@ -68,7 +69,6 @@ describe('processRequest', () => {
|
||||||
|
|
||||||
Bundler.prototype.invalidateFile = invalidatorFunc;
|
Bundler.prototype.invalidateFile = invalidatorFunc;
|
||||||
|
|
||||||
const Server = require('../');
|
|
||||||
server = new Server(options);
|
server = new Server(options);
|
||||||
requestHandler = server.processRequest.bind(server);
|
requestHandler = server.processRequest.bind(server);
|
||||||
});
|
});
|
||||||
|
@ -172,7 +172,6 @@ describe('processRequest', () => {
|
||||||
|
|
||||||
Bundler.prototype.bundle = bundleFunc;
|
Bundler.prototype.bundle = bundleFunc;
|
||||||
|
|
||||||
const Server = require('../../Server');
|
|
||||||
server = new Server(options);
|
server = new Server(options);
|
||||||
|
|
||||||
requestHandler = server.processRequest.bind(server);
|
requestHandler = server.processRequest.bind(server);
|
||||||
|
@ -232,11 +231,6 @@ describe('processRequest', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/assets endpoint', () => {
|
describe('/assets endpoint', () => {
|
||||||
var AssetServer;
|
|
||||||
beforeEach(() => {
|
|
||||||
AssetServer = require('../../AssetServer');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should serve simple case', () => {
|
it('should serve simple case', () => {
|
||||||
const req = {url: '/assets/imgs/a.png'};
|
const req = {url: '/assets/imgs/a.png'};
|
||||||
const res = {end: jest.genMockFn()};
|
const res = {end: jest.genMockFn()};
|
||||||
|
|
|
@ -13,26 +13,27 @@ jest.setMock('worker-farm', function() { return () => {}; })
|
||||||
.mock('net')
|
.mock('net')
|
||||||
.dontMock('../SocketClient');
|
.dontMock('../SocketClient');
|
||||||
|
|
||||||
|
var Bundle = require('../../Bundler/Bundle');
|
||||||
|
var SocketClient = require('../SocketClient');
|
||||||
|
var bser = require('bser');
|
||||||
|
var net = require('net');
|
||||||
|
|
||||||
describe('SocketClient', () => {
|
describe('SocketClient', () => {
|
||||||
let SocketClient;
|
|
||||||
let sock;
|
let sock;
|
||||||
let bunser;
|
let bunser;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
SocketClient = require('../SocketClient');
|
|
||||||
|
|
||||||
const {EventEmitter} = require.requireActual('events');
|
const {EventEmitter} = require.requireActual('events');
|
||||||
sock = new EventEmitter();
|
sock = new EventEmitter();
|
||||||
sock.write = jest.genMockFn();
|
sock.write = jest.genMockFn();
|
||||||
|
|
||||||
require('net').connect.mockImpl(() => sock);
|
net.connect.mockImpl(() => sock);
|
||||||
|
|
||||||
const bser = require('bser');
|
|
||||||
bunser = new EventEmitter();
|
bunser = new EventEmitter();
|
||||||
require('bser').BunserBuf.mockImpl(() => bunser);
|
bser.BunserBuf.mockImpl(() => bunser);
|
||||||
bser.dumpToBuffer.mockImpl((a) => a);
|
bser.dumpToBuffer.mockImpl((a) => a);
|
||||||
|
|
||||||
require('../../Bundler/Bundle').fromJSON.mockImpl((a) => a);
|
Bundle.fromJSON.mockImpl((a) => a);
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('create a connection', () => {
|
pit('create a connection', () => {
|
||||||
|
@ -40,7 +41,7 @@ describe('SocketClient', () => {
|
||||||
sock.emit('connect');
|
sock.emit('connect');
|
||||||
return client.onReady().then(c => {
|
return client.onReady().then(c => {
|
||||||
expect(c).toBe(client);
|
expect(c).toBe(client);
|
||||||
expect(require('net').connect).toBeCalledWith('/sock');
|
expect(net.connect).toBeCalledWith('/sock');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,23 +13,19 @@ jest.setMock('worker-farm', function() { return () => {}; })
|
||||||
.mock('child_process')
|
.mock('child_process')
|
||||||
.dontMock('../');
|
.dontMock('../');
|
||||||
|
|
||||||
|
var SocketInterface = require('../');
|
||||||
|
var SocketClient = require('../SocketClient');
|
||||||
|
var childProcess = require('child_process');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
describe('SocketInterface', () => {
|
describe('SocketInterface', () => {
|
||||||
let SocketInterface;
|
|
||||||
let SocketClient;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
SocketInterface = require('../');
|
|
||||||
SocketClient = require('../SocketClient');
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('getOrCreateSocketFor', () => {
|
describe('getOrCreateSocketFor', () => {
|
||||||
pit('creates socket path by hashing options', () => {
|
pit('creates socket path by hashing options', () => {
|
||||||
const fs = require('fs');
|
|
||||||
fs.existsSync = jest.genMockFn().mockImpl(() => true);
|
fs.existsSync = jest.genMockFn().mockImpl(() => true);
|
||||||
fs.unlinkSync = jest.genMockFn();
|
fs.unlinkSync = jest.genMockFn();
|
||||||
let callback;
|
let callback;
|
||||||
|
|
||||||
require('child_process').spawn.mockImpl(() => ({
|
childProcess.spawn.mockImpl(() => ({
|
||||||
on: (event, cb) => callback = cb,
|
on: (event, cb) => callback = cb,
|
||||||
send: (message) => {
|
send: (message) => {
|
||||||
setImmediate(() => callback({ type: 'createdServer' }));
|
setImmediate(() => callback({ type: 'createdServer' }));
|
||||||
|
@ -58,13 +54,12 @@ describe('SocketInterface', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('should fork a server', () => {
|
pit('should fork a server', () => {
|
||||||
const fs = require('fs');
|
|
||||||
fs.existsSync = jest.genMockFn().mockImpl(() => false);
|
fs.existsSync = jest.genMockFn().mockImpl(() => false);
|
||||||
fs.unlinkSync = jest.genMockFn();
|
fs.unlinkSync = jest.genMockFn();
|
||||||
let sockPath;
|
let sockPath;
|
||||||
let callback;
|
let callback;
|
||||||
|
|
||||||
require('child_process').spawn.mockImpl(() => ({
|
childProcess.spawn.mockImpl(() => ({
|
||||||
on: (event, cb) => callback = cb,
|
on: (event, cb) => callback = cb,
|
||||||
send: (message) => {
|
send: (message) => {
|
||||||
expect(message.type).toBe('createSocketServer');
|
expect(message.type).toBe('createSocketServer');
|
||||||
|
|
|
@ -14,26 +14,24 @@ jest.setMock('worker-farm', function() { return () => {}; })
|
||||||
.mock('fs')
|
.mock('fs')
|
||||||
.dontMock('../SocketServer');
|
.dontMock('../SocketServer');
|
||||||
|
|
||||||
|
var PackagerServer = require('../../Server');
|
||||||
|
var SocketServer = require('../SocketServer');
|
||||||
|
var bser = require('bser');
|
||||||
|
var net = require('net');
|
||||||
|
|
||||||
describe('SocketServer', () => {
|
describe('SocketServer', () => {
|
||||||
let PackagerServer;
|
|
||||||
let SocketServer;
|
|
||||||
let netServer;
|
let netServer;
|
||||||
let bunser;
|
let bunser;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
SocketServer = require('../SocketServer');
|
|
||||||
|
|
||||||
const {EventEmitter} = require.requireActual('events');
|
const {EventEmitter} = require.requireActual('events');
|
||||||
netServer = new EventEmitter();
|
netServer = new EventEmitter();
|
||||||
netServer.listen = jest.genMockFn();
|
netServer.listen = jest.genMockFn();
|
||||||
require('net').createServer.mockImpl(() => netServer);
|
net.createServer.mockImpl(() => netServer);
|
||||||
|
|
||||||
const bser = require('bser');
|
|
||||||
bunser = new EventEmitter();
|
bunser = new EventEmitter();
|
||||||
bser.BunserBuf.mockImpl(() => bunser);
|
bser.BunserBuf.mockImpl(() => bunser);
|
||||||
bser.dumpToBuffer.mockImpl((a) => a);
|
bser.dumpToBuffer.mockImpl((a) => a);
|
||||||
|
|
||||||
PackagerServer = require('../../Server');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
pit('create a server', () => {
|
pit('create a server', () => {
|
||||||
|
|
|
@ -11,13 +11,9 @@
|
||||||
jest.dontMock('../getPlatformExtension')
|
jest.dontMock('../getPlatformExtension')
|
||||||
.dontMock('../getAssetDataFromName');
|
.dontMock('../getAssetDataFromName');
|
||||||
|
|
||||||
|
var getAssetDataFromName = require('../getAssetDataFromName');
|
||||||
|
|
||||||
describe('getAssetDataFromName', () => {
|
describe('getAssetDataFromName', () => {
|
||||||
let getAssetDataFromName;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
getAssetDataFromName = require('../getAssetDataFromName');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get data from name', () => {
|
it('should get data from name', () => {
|
||||||
expect(getAssetDataFromName('a/b/c.png')).toEqual({
|
expect(getAssetDataFromName('a/b/c.png')).toEqual({
|
||||||
resolution: 1,
|
resolution: 1,
|
||||||
|
|
|
@ -10,9 +10,10 @@
|
||||||
|
|
||||||
jest.dontMock('../getPlatformExtension');
|
jest.dontMock('../getPlatformExtension');
|
||||||
|
|
||||||
|
var getPlatformExtension = require('../getPlatformExtension');
|
||||||
|
|
||||||
describe('getPlatformExtension', function() {
|
describe('getPlatformExtension', function() {
|
||||||
it('should get platform ext', function() {
|
it('should get platform ext', function() {
|
||||||
var getPlatformExtension = require('../getPlatformExtension');
|
|
||||||
expect(getPlatformExtension('a.ios.js')).toBe('ios');
|
expect(getPlatformExtension('a.ios.js')).toBe('ios');
|
||||||
expect(getPlatformExtension('a.android.js')).toBe('android');
|
expect(getPlatformExtension('a.android.js')).toBe('android');
|
||||||
expect(getPlatformExtension('/b/c/a.ios.js')).toBe('ios');
|
expect(getPlatformExtension('/b/c/a.ios.js')).toBe('ios');
|
||||||
|
|
Loading…
Reference in New Issue