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();
|
||||
|
||||
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!');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -11,17 +11,11 @@ jest
|
|||
|
||||
const Promise = require('promise');
|
||||
|
||||
var AssetServer = require('../');
|
||||
var crypto = require('crypto');
|
||||
var fs = require('fs');
|
||||
|
||||
describe('AssetServer', () => {
|
||||
let AssetServer;
|
||||
let crypto;
|
||||
let fs;
|
||||
|
||||
beforeEach(() => {
|
||||
AssetServer = require('../');
|
||||
crypto = require('crypto');
|
||||
fs = require('fs');
|
||||
});
|
||||
|
||||
describe('assetServer.get', () => {
|
||||
pit('should work for the simple case', () => {
|
||||
const server = new AssetServer({
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 });
|
||||
});
|
||||
|
||||
|
|
|
@ -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'],
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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: {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -19,24 +19,16 @@ jest
|
|||
jest
|
||||
.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', () => {
|
||||
var Fastfs;
|
||||
var Module;
|
||||
var ModuleCache;
|
||||
var Promise;
|
||||
var fs;
|
||||
|
||||
const FileWatcher = require('../../FileWatcher');
|
||||
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(
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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()};
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -13,23 +13,19 @@ jest.setMock('worker-farm', function() { return () => {}; })
|
|||
.mock('child_process')
|
||||
.dontMock('../');
|
||||
|
||||
var SocketInterface = require('../');
|
||||
var SocketClient = require('../SocketClient');
|
||||
var childProcess = require('child_process');
|
||||
var fs = require('fs');
|
||||
|
||||
describe('SocketInterface', () => {
|
||||
let SocketInterface;
|
||||
let SocketClient;
|
||||
|
||||
beforeEach(() => {
|
||||
SocketInterface = require('../');
|
||||
SocketClient = require('../SocketClient');
|
||||
});
|
||||
|
||||
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');
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -11,13 +11,9 @@
|
|||
jest.dontMock('../getPlatformExtension')
|
||||
.dontMock('../getAssetDataFromName');
|
||||
|
||||
var getAssetDataFromName = require('../getAssetDataFromName');
|
||||
|
||||
describe('getAssetDataFromName', () => {
|
||||
let getAssetDataFromName;
|
||||
|
||||
beforeEach(() => {
|
||||
getAssetDataFromName = require('../getAssetDataFromName');
|
||||
});
|
||||
|
||||
it('should get data from name', () => {
|
||||
expect(getAssetDataFromName('a/b/c.png')).toEqual({
|
||||
resolution: 1,
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in New Issue