`@format` for `Graph-test.js`

Reviewed By: jeanlauliac

Differential Revision: D5857069

fbshipit-source-id: 38e58f11579161a94d5201d016cf8b0ba3681eb4
This commit is contained in:
David Aurelio 2017-09-19 09:18:51 -07:00 committed by Facebook Github Bot
parent 6062a9bd34
commit 5aaf148179
1 changed files with 147 additions and 97 deletions

View File

@ -5,12 +5,13 @@
* This source code is licensed under the BSD-style license found in the * This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant * LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+javascript_foundation
* @format
*/ */
'use strict'; 'use strict';
jest jest.useRealTimers().mock('console');
.useRealTimers()
.mock('console');
const {Console} = require('console'); const {Console} = require('console');
const Graph = require('../Graph'); const Graph = require('../Graph');
@ -45,7 +46,11 @@ describe('Graph:', () => {
const entryPoint = '/arbitrary/path'; const entryPoint = '/arbitrary/path';
graph([entryPoint], anyPlatform, noOpts, () => { graph([entryPoint], anyPlatform, noOpts, () => {
expect(resolve).toBeCalledWith( expect(resolve).toBeCalledWith(
entryPoint, null, any(String), any(Object)); entryPoint,
null,
any(String),
any(Object),
);
done(); done();
}); });
}); });
@ -54,12 +59,19 @@ describe('Graph:', () => {
const entryPoints = ['Arbitrary', '../entry.js']; const entryPoints = ['Arbitrary', '../entry.js'];
graph(entryPoints, anyPlatform, noOpts, () => { graph(entryPoints, anyPlatform, noOpts, () => {
expect(resolve).toBeCalledWith( expect(resolve).toBeCalledWith(
entryPoints[0], null, any(String), any(Object)); entryPoints[0],
null,
any(String),
any(Object),
);
expect(resolve).toBeCalledWith( expect(resolve).toBeCalledWith(
entryPoints[1], null, any(String), any(Object)); entryPoints[1],
null,
any(String),
any(Object),
);
done(); done();
}); });
}); });
it('calls back with an error when called without `platform` option', done => { it('calls back with an error when called without `platform` option', done => {
@ -72,8 +84,7 @@ describe('Graph:', () => {
it('forwards a passed-in `platform` to `resolve`', done => { it('forwards a passed-in `platform` to `resolve`', done => {
const platform = 'any'; const platform = 'any';
graph(anyEntry, platform, noOpts, () => { graph(anyEntry, platform, noOpts, () => {
expect(resolve).toBeCalledWith( expect(resolve).toBeCalledWith(any(String), null, platform, any(Object));
any(String), null, platform, any(Object));
done(); done();
}); });
}); });
@ -82,7 +93,11 @@ describe('Graph:', () => {
const log = new Console(); const log = new Console();
graph(anyEntry, anyPlatform, {log}, () => { graph(anyEntry, anyPlatform, {log}, () => {
expect(resolve).toBeCalledWith( expect(resolve).toBeCalledWith(
any(String), null, any(String), objectContaining({log})); any(String),
null,
any(String),
objectContaining({log}),
);
done(); done();
}); });
}); });
@ -99,11 +114,13 @@ describe('Graph:', () => {
it('only calls back once if two parallel invocations of `resolve` fail', done => { it('only calls back once if two parallel invocations of `resolve` fail', done => {
load.stub.returns({ load.stub.returns({
file: createFile('with two deps'), file: createFile('with two deps'),
dependencies: ['depA', 'depB']}, dependencies: ['depA', 'depB'],
); });
resolve.stub resolve.stub
.withArgs('depA').throws(new Error()) .withArgs('depA')
.withArgs('depB').throws(new Error()); .throws(new Error())
.withArgs('depB')
.throws(new Error());
let calls = 0; let calls = 0;
function callback() { function callback() {
@ -139,7 +156,9 @@ describe('Graph:', () => {
it('passes the `optimize` flag on to `load`', done => { it('passes the `optimize` flag on to `load`', done => {
graph(anyEntry, anyPlatform, {optimize: true}, () => { graph(anyEntry, anyPlatform, {optimize: true}, () => {
expect(load).toBeCalledWith( expect(load).toBeCalledWith(
any(String), objectContaining({optimize: true})); any(String),
objectContaining({optimize: true}),
);
done(); done();
}); });
}); });
@ -147,7 +166,9 @@ describe('Graph:', () => {
it('uses `false` as the default for the `optimize` flag', done => { it('uses `false` as the default for the `optimize` flag', done => {
graph(anyEntry, anyPlatform, noOpts, () => { graph(anyEntry, anyPlatform, noOpts, () => {
expect(load).toBeCalledWith( expect(load).toBeCalledWith(
any(String), objectContaining({optimize: false})); any(String),
objectContaining({optimize: false}),
);
done(); done();
}); });
}); });
@ -155,8 +176,7 @@ describe('Graph:', () => {
it('forwards a passed-in `log` to `load`', done => { it('forwards a passed-in `log` to `load`', done => {
const log = new Console(); const log = new Console();
graph(anyEntry, anyPlatform, {log}, () => { graph(anyEntry, anyPlatform, {log}, () => {
expect(load) expect(load).toBeCalledWith(any(String), objectContaining({log}));
.toBeCalledWith(any(String), objectContaining({log}));
done(); done();
}); });
}); });
@ -181,10 +201,8 @@ describe('Graph:', () => {
}); });
graph(['entry'], anyPlatform, noOpts, () => { graph(['entry'], anyPlatform, noOpts, () => {
expect(resolve).toBeCalledWith( expect(resolve).toBeCalledWith(id1, entryPath, any(String), any(Object));
id1, entryPath, any(String), any(Object)); expect(resolve).toBeCalledWith(id2, entryPath, any(String), any(Object));
expect(resolve).toBeCalledWith(
id2, entryPath, any(String), any(Object));
done(); done();
}); });
}); });
@ -197,12 +215,17 @@ describe('Graph:', () => {
const path2 = '/path/to/dep/2'; const path2 = '/path/to/dep/2';
resolve.stub resolve.stub
.withArgs(id1).returns(path1) .withArgs(id1)
.withArgs(id2).returns(path2) .returns(path1)
.withArgs('entry').returns(entryPath); .withArgs(id2)
.returns(path2)
.withArgs('entry')
.returns(entryPath);
load.stub load.stub
.withArgs(entryPath).returns({file: {path: entryPath}, dependencies: [id1]}) .withArgs(entryPath)
.withArgs(path1).returns({file: {path: path1}, dependencies: [id2]}); .returns({file: {path: entryPath}, dependencies: [id1]})
.withArgs(path1)
.returns({file: {path: path1}, dependencies: [id2]});
graph(['entry'], anyPlatform, noOpts, () => { graph(['entry'], anyPlatform, noOpts, () => {
expect(resolve).toBeCalledWith(id2, path1, any(String), any(Object)); expect(resolve).toBeCalledWith(id2, path1, any(String), any(Object));
@ -212,69 +235,75 @@ describe('Graph:', () => {
}); });
}); });
it('resolves modules in depth-first traversal order, regardless of the order of loading', it('resolves modules in depth-first traversal order, regardless of the order of loading', done => {
done => { load.stub.reset();
load.stub.reset(); resolve.stub.reset();
resolve.stub.reset();
const ids = [ const ids = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
'a', ids.forEach(id => {
'b', const path = idToPath(id);
'c', 'd', resolve.stub.withArgs(id).returns(path);
'e',
'f', 'g',
'h',
];
ids.forEach(id => {
const path = idToPath(id);
resolve.stub.withArgs(id).returns(path);
load.stub.withArgs(path).returns({file: createFile(id), dependencies: []});
});
load.stub.withArgs(idToPath('a')).returns({file: createFile('a'), dependencies: ['b', 'e', 'h']});
// load certain files later
const b = deferred({file: createFile('b'), dependencies: ['c', 'd']});
const e = deferred({file: createFile('e'), dependencies: ['f', 'g']});
load.stub load.stub
.withArgs(idToPath('b')).returns(b.promise) .withArgs(path)
.withArgs(idToPath('e')).returns(e.promise) .returns({file: createFile(id), dependencies: []});
.withArgs(idToPath('h')).func = (f, o) => { });
process.nextTick(() => { load.stub
// `e` loads after `h` .withArgs(idToPath('a'))
e.resolve(); .returns({file: createFile('a'), dependencies: ['b', 'e', 'h']});
// `b` loads after `a`
process.nextTick(b.resolve);
});
return {file: createFile('h'), dependencies: []};
};
graph(['a'], anyPlatform, noOpts, (error, result) => { // load certain files later
expect(error).toEqual(null); const b = deferred({file: createFile('b'), dependencies: ['c', 'd']});
expect(result.modules).toEqual([ const e = deferred({file: createFile('e'), dependencies: ['f', 'g']});
createModule('a', ['b', 'e', 'h']), load.stub
createModule('b', ['c', 'd']), .withArgs(idToPath('b'))
createModule('c'), .returns(b.promise)
createModule('d'), .withArgs(idToPath('e'))
createModule('e', ['f', 'g']), .returns(e.promise)
createModule('f'), .withArgs(idToPath('h')).func = (f, o) => {
createModule('g'), process.nextTick(() => {
createModule('h'), // `e` loads after `h`
]); e.resolve();
done(); // `b` loads after `a`
process.nextTick(b.resolve);
}); });
}, return {file: createFile('h'), dependencies: []};
); };
graph(['a'], anyPlatform, noOpts, (error, result) => {
expect(error).toEqual(null);
expect(result.modules).toEqual([
createModule('a', ['b', 'e', 'h']),
createModule('b', ['c', 'd']),
createModule('c'),
createModule('d'),
createModule('e', ['f', 'g']),
createModule('f'),
createModule('g'),
createModule('h'),
]);
done();
});
});
it('calls back with the resolved modules of the entry points', done => { it('calls back with the resolved modules of the entry points', done => {
load.stub.reset(); load.stub.reset();
resolve.stub.reset(); resolve.stub.reset();
load.stub.withArgs(idToPath('a')).returns({file: createFile('a'), dependencies: ['b']}); load.stub
load.stub.withArgs(idToPath('b')).returns({file: createFile('b'), dependencies: []}); .withArgs(idToPath('a'))
load.stub.withArgs(idToPath('c')).returns({file: createFile('c'), dependencies: ['d']}); .returns({file: createFile('a'), dependencies: ['b']});
load.stub.withArgs(idToPath('d')).returns({file: createFile('d'), dependencies: []}); load.stub
.withArgs(idToPath('b'))
.returns({file: createFile('b'), dependencies: []});
load.stub
.withArgs(idToPath('c'))
.returns({file: createFile('c'), dependencies: ['d']});
load.stub
.withArgs(idToPath('d'))
.returns({file: createFile('d'), dependencies: []});
'abcd'.split('') 'abcd'
.split('')
.forEach(id => resolve.stub.withArgs(id).returns(idToPath(id))); .forEach(id => resolve.stub.withArgs(id).returns(idToPath(id)));
graph(['a', 'c'], anyPlatform, noOpts, (error, result) => { graph(['a', 'c'], anyPlatform, noOpts, (error, result) => {
@ -290,10 +319,15 @@ describe('Graph:', () => {
load.stub.reset(); load.stub.reset();
resolve.stub.reset(); resolve.stub.reset();
load.stub.withArgs(idToPath('a')).returns({file: createFile('a'), dependencies: ['b']}); load.stub
load.stub.withArgs(idToPath('b')).returns({file: createFile('b'), dependencies: []}); .withArgs(idToPath('a'))
.returns({file: createFile('a'), dependencies: ['b']});
load.stub
.withArgs(idToPath('b'))
.returns({file: createFile('b'), dependencies: []});
'ab'.split('') 'ab'
.split('')
.forEach(id => resolve.stub.withArgs(id).returns(idToPath(id))); .forEach(id => resolve.stub.withArgs(id).returns(idToPath(id)));
graph(['a', 'b'], anyPlatform, noOpts, (error, result) => { graph(['a', 'b'], anyPlatform, noOpts, (error, result) => {
@ -310,11 +344,15 @@ describe('Graph:', () => {
ids.forEach(id => { ids.forEach(id => {
const path = idToPath(id); const path = idToPath(id);
resolve.stub.withArgs(id).returns(path); resolve.stub.withArgs(id).returns(path);
load.stub.withArgs(path).returns({file: createFile(id), dependencies: []}); load.stub
.withArgs(path)
.returns({file: createFile(id), dependencies: []});
}); });
['a', 'd'].forEach(id => ['a', 'd'].forEach(id =>
load.stub load.stub
.withArgs(idToPath(id)).returns({file: createFile(id), dependencies: ['b', 'c']})); .withArgs(idToPath(id))
.returns({file: createFile(id), dependencies: ['b', 'c']}),
);
graph(['a', 'd', 'b'], anyPlatform, noOpts, (error, result) => { graph(['a', 'd', 'b'], anyPlatform, noOpts, (error, result) => {
expect(error).toEqual(null); expect(error).toEqual(null);
@ -330,13 +368,19 @@ describe('Graph:', () => {
it('handles dependency cycles', done => { it('handles dependency cycles', done => {
resolve.stub resolve.stub
.withArgs('a').returns(idToPath('a')) .withArgs('a')
.withArgs('b').returns(idToPath('b')) .returns(idToPath('a'))
.withArgs('c').returns(idToPath('c')); .withArgs('b')
.returns(idToPath('b'))
.withArgs('c')
.returns(idToPath('c'));
load.stub load.stub
.withArgs(idToPath('a')).returns({file: createFile('a'), dependencies: ['b']}) .withArgs(idToPath('a'))
.withArgs(idToPath('b')).returns({file: createFile('b'), dependencies: ['c']}) .returns({file: createFile('a'), dependencies: ['b']})
.withArgs(idToPath('c')).returns({file: createFile('c'), dependencies: ['a']}); .withArgs(idToPath('b'))
.returns({file: createFile('b'), dependencies: ['c']})
.withArgs(idToPath('c'))
.returns({file: createFile('c'), dependencies: ['a']});
graph(['a'], anyPlatform, noOpts, (error, result) => { graph(['a'], anyPlatform, noOpts, (error, result) => {
expect(result.modules).toEqual([ expect(result.modules).toEqual([
@ -349,13 +393,19 @@ describe('Graph:', () => {
}); });
it('can skip files', done => { it('can skip files', done => {
['a', 'b', 'c', 'd', 'e'].forEach( ['a', 'b', 'c', 'd', 'e'].forEach(id =>
id => resolve.stub.withArgs(id).returns(idToPath(id))); resolve.stub.withArgs(id).returns(idToPath(id)),
);
load.stub load.stub
.withArgs(idToPath('a')).returns({file: createFile('a'), dependencies: ['b', 'c', 'd']}) .withArgs(idToPath('a'))
.withArgs(idToPath('b')).returns({file: createFile('b'), dependencies: ['e']}); .returns({file: createFile('a'), dependencies: ['b', 'c', 'd']})
.withArgs(idToPath('b'))
.returns({file: createFile('b'), dependencies: ['e']});
['c', 'd', 'e'].forEach(id => ['c', 'd', 'e'].forEach(id =>
load.stub.withArgs(idToPath(id)).returns({file: createFile(id), dependencies: []})); load.stub
.withArgs(idToPath(id))
.returns({file: createFile(id), dependencies: []}),
);
const skip = new Set([idToPath('b'), idToPath('c')]); const skip = new Set([idToPath('b'), idToPath('c')]);
graph(['a'], anyPlatform, {skip}, (error, result) => { graph(['a'], anyPlatform, {skip}, (error, result) => {
@ -389,6 +439,6 @@ function idToPath(id) {
function deferred(value) { function deferred(value) {
let resolve; let resolve;
const promise = new Promise(res => resolve = res); const promise = new Promise(res => (resolve = res));
return {promise, resolve: () => resolve(value)}; return {promise, resolve: () => resolve(value)};
} }