From f8a724121bf1710ebb0ed27660b0e08740a68ed5 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Tue, 23 May 2017 04:52:46 -0700 Subject: [PATCH] Add @format to a few files Reviewed By: davidaurelio Differential Revision: D5111297 fbshipit-source-id: bde11df412dd694edca78d6a61f4c69e5abba60a --- .../src/Bundler/__tests__/Bundle-test.js | 490 +- .../worker/__tests__/transform-module-test.js | 142 +- .../ModuleGraph/worker/transform-module.js | 24 +- .../symbolicate/__tests__/worker-test.js | 43 +- .../__tests__/DependencyGraph-test.js | 5655 +++++++++-------- 5 files changed, 3343 insertions(+), 3011 deletions(-) diff --git a/packages/metro-bundler/src/Bundler/__tests__/Bundle-test.js b/packages/metro-bundler/src/Bundler/__tests__/Bundle-test.js index 134abf6d..e03716e4 100644 --- a/packages/metro-bundler/src/Bundler/__tests__/Bundle-test.js +++ b/packages/metro-bundler/src/Bundler/__tests__/Bundle-test.js @@ -5,6 +5,8 @@ * 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 * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ 'use strict'; @@ -26,117 +28,128 @@ describe('Bundle', () => { describe('source bundle', () => { it('should create a bundle and get the source', () => { - return Promise.resolve().then(() => { - return addModule({ - bundle, - code: 'transformed foo;', - sourceCode: 'source foo', - sourcePath: 'foo path', + return Promise.resolve() + .then(() => { + return addModule({ + bundle, + code: 'transformed foo;', + sourceCode: 'source foo', + sourcePath: 'foo path', + }); + }) + .then(() => { + return addModule({ + bundle, + code: 'transformed bar;', + sourceCode: 'source bar', + sourcePath: 'bar path', + }); + }) + .then(() => { + bundle.finalize({}); + expect(bundle.getSource({dev: true})).toBe( + [ + 'transformed foo;', + 'transformed bar;', + '\/\/# sourceMappingURL=test_url', + ].join('\n'), + ); }); - }).then(() => { - return addModule({ - bundle, - code: 'transformed bar;', - sourceCode: 'source bar', - sourcePath: 'bar path', - }); - }).then(() => { - bundle.finalize({}); - expect(bundle.getSource({dev: true})).toBe([ - 'transformed foo;', - 'transformed bar;', - '\/\/# sourceMappingURL=test_url', - ].join('\n')); - }); }); it('should be ok to leave out the source map url', () => { const otherBundle = new Bundle(); - return Promise.resolve().then(() => { - return addModule({ - bundle: otherBundle, - code: 'transformed foo;', - sourceCode: 'source foo', - sourcePath: 'foo path', + return Promise.resolve() + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'transformed foo;', + sourceCode: 'source foo', + sourcePath: 'foo path', + }); + }) + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'transformed bar;', + sourceCode: 'source bar', + sourcePath: 'bar path', + }); + }) + .then(() => { + otherBundle.finalize({}); + expect(otherBundle.getSource({dev: true})).toBe( + ['transformed foo;', 'transformed bar;'].join('\n'), + ); }); - }).then(() => { - return addModule({ - bundle: otherBundle, - code: 'transformed bar;', - sourceCode: 'source bar', - sourcePath: 'bar path', - }); - }).then(() => { - otherBundle.finalize({}); - expect(otherBundle.getSource({dev: true})).toBe([ - 'transformed foo;', - 'transformed bar;', - ].join('\n')); - }); }); it('should create a bundle and add run module code', () => { - return Promise.resolve().then(() => { - return addModule({ - bundle, - code: 'transformed foo;', - sourceCode: 'source foo', - sourcePath: 'foo path', + return Promise.resolve() + .then(() => { + return addModule({ + bundle, + code: 'transformed foo;', + sourceCode: 'source foo', + sourcePath: 'foo path', + }); + }) + .then(() => { + return addModule({ + bundle, + code: 'transformed bar;', + sourceCode: 'source bar', + sourcePath: 'bar path', + }); + }) + .then(() => { + bundle.setMainModuleId('foo'); + bundle.finalize({ + runBeforeMainModule: ['bar'], + runModule: true, + }); + expect(bundle.getSource({dev: true})).toBe( + [ + 'transformed foo;', + 'transformed bar;', + ';require("bar");', + ';require("foo");', + '\/\/# sourceMappingURL=test_url', + ].join('\n'), + ); }); - }).then(() => { - return addModule({ - bundle, - code: 'transformed bar;', - sourceCode: 'source bar', - sourcePath: 'bar path', - }); - }).then(() => { - bundle.setMainModuleId('foo'); - bundle.finalize({ - runBeforeMainModule: ['bar'], - runModule: true, - }); - expect(bundle.getSource({dev: true})).toBe([ - 'transformed foo;', - 'transformed bar;', - ';require("bar");', - ';require("foo");', - '\/\/# sourceMappingURL=test_url', - ].join('\n')); - }); }); - it('inserts modules in a deterministic order, independent of timing of the wrapper process', - () => { - const moduleTransports = [ - createModuleTransport({name: 'module1'}), - createModuleTransport({name: 'module2'}), - createModuleTransport({name: 'module3'}), - ]; + it('inserts modules in a deterministic order, independent of timing of the wrapper process', () => { + const moduleTransports = [ + createModuleTransport({name: 'module1'}), + createModuleTransport({name: 'module2'}), + createModuleTransport({name: 'module3'}), + ]; - const resolves = {}; - const resolver = { - wrapModule({name}) { - return new Promise(resolve => { - resolves[name] = resolve; - }); - }, - }; + const resolves = {}; + const resolver = { + wrapModule({name}) { + return new Promise(resolve => { + resolves[name] = resolve; + }); + }, + }; - const promise = Promise.all(moduleTransports.map( - m => bundle.addModule(resolver, null, {isPolyfill: () => false}, m) - )).then(() => { - expect(bundle.getModules()) - .toEqual(moduleTransports); - }); + const promise = Promise.all( + moduleTransports.map(m => + bundle.addModule(resolver, null, {isPolyfill: () => false}, m), + ), + ).then(() => { + expect(bundle.getModules()).toEqual(moduleTransports); + }); - resolves.module2({code: ''}); - resolves.module3({code: ''}); - resolves.module1({code: ''}); + resolves.module2({code: ''}); + resolves.module3({code: ''}); + resolves.module1({code: ''}); - return promise; - }, - ); + return promise; + }); }); describe('sourcemap bundle', () => { @@ -147,89 +160,93 @@ describe('Bundle', () => { it('should combine sourcemaps', () => { const otherBundle = new Bundle({sourceMapUrl: 'test_url'}); - return Promise.resolve().then(() => { - return addModule({ - bundle: otherBundle, - code: 'transformed foo;\n', - sourceCode: 'source foo', - map: {name: 'sourcemap foo'}, - sourcePath: 'foo path', - }); - }).then(() => { - return addModule({ - bundle: otherBundle, - code: 'transformed bar;\n', - sourceCode: 'source bar', - map: {name: 'sourcemap bar'}, - sourcePath: 'bar path', - }); - }).then(() => { - return addModule({ - bundle: otherBundle, - code: 'image module;\nimage module;', - virtual: true, - sourceCode: 'image module;\nimage module;', - sourcePath: 'image.png', - }); - }).then(() => { - otherBundle.setMainModuleId('foo'); - otherBundle.finalize({ - runBeforeMainModule: ['InitializeCore'], - runModule: true, - }); + return Promise.resolve() + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'transformed foo;\n', + sourceCode: 'source foo', + map: {name: 'sourcemap foo'}, + sourcePath: 'foo path', + }); + }) + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'transformed bar;\n', + sourceCode: 'source bar', + map: {name: 'sourcemap bar'}, + sourcePath: 'bar path', + }); + }) + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'image module;\nimage module;', + virtual: true, + sourceCode: 'image module;\nimage module;', + sourcePath: 'image.png', + }); + }) + .then(() => { + otherBundle.setMainModuleId('foo'); + otherBundle.finalize({ + runBeforeMainModule: ['InitializeCore'], + runModule: true, + }); - const sourceMap = otherBundle.getSourceMap({dev: true}); - expect(sourceMap).toEqual({ - file: 'test_url', - version: 3, - sections: [ - {offset: {line: 0, column: 0}, map: {name: 'sourcemap foo'}}, - {offset: {line: 2, column: 0}, map: {name: 'sourcemap bar'}}, - { - offset: { - column: 0, - line: 4, + const sourceMap = otherBundle.getSourceMap({dev: true}); + expect(sourceMap).toEqual({ + file: 'test_url', + version: 3, + sections: [ + {offset: {line: 0, column: 0}, map: {name: 'sourcemap foo'}}, + {offset: {line: 2, column: 0}, map: {name: 'sourcemap bar'}}, + { + offset: { + column: 0, + line: 4, + }, + map: { + file: 'image.png', + mappings: 'AAAA;AACA;', + names: [], + sources: ['image.png'], + sourcesContent: ['image module;\nimage module;'], + version: 3, + }, }, - map: { - file: 'image.png', - mappings: 'AAAA;AACA;', - names: [], - sources: ['image.png'], - sourcesContent: ['image module;\nimage module;'], - version: 3, + { + offset: { + column: 0, + line: 6, + }, + map: { + file: 'require-InitializeCore.js', + mappings: 'AAAA;', + names: [], + sources: ['require-InitializeCore.js'], + sourcesContent: [';require("InitializeCore");'], + version: 3, + }, }, - }, - { - offset: { - column: 0, - line: 6, + { + offset: { + column: 0, + line: 7, + }, + map: { + file: 'require-foo.js', + mappings: 'AAAA;', + names: [], + sources: ['require-foo.js'], + sourcesContent: [';require("foo");'], + version: 3, + }, }, - map: { - file: 'require-InitializeCore.js', - mappings: 'AAAA;', - names: [], - sources: ['require-InitializeCore.js'], - sourcesContent: [';require("InitializeCore");'], - version: 3, - }, - }, - { - offset: { - column: 0, - line: 7, - }, - map: { - file: 'require-foo.js', - mappings: 'AAAA;', - names: [], - sources: ['require-foo.js'], - sourcesContent: [';require("foo");'], - version: 3, - }, - }, - ], + ], + }); }); - }); }); }); @@ -248,31 +265,37 @@ describe('Bundle', () => { describe('getJSModulePaths()', () => { it('should return module paths', () => { var otherBundle = new Bundle({sourceMapUrl: 'test_url'}); - return Promise.resolve().then(() => { - return addModule({ - bundle: otherBundle, - code: 'transformed foo;\n', - sourceCode: 'source foo', - sourcePath: 'foo path', + return Promise.resolve() + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'transformed foo;\n', + sourceCode: 'source foo', + sourcePath: 'foo path', + }); + }) + .then(() => { + return addModule({ + bundle: otherBundle, + code: 'image module;\nimage module;', + virtual: true, + sourceCode: 'image module;\nimage module;', + sourcePath: 'image.png', + }); + }) + .then(() => { + expect(otherBundle.getJSModulePaths()).toEqual(['foo path']); }); - }).then(() => { - return addModule({ - bundle: otherBundle, - code: 'image module;\nimage module;', - virtual: true, - sourceCode: 'image module;\nimage module;', - sourcePath: 'image.png', - }); - }).then(() => { - expect(otherBundle.getJSModulePaths()).toEqual(['foo path']); - }); }); }); describe('getEtag()', function() { it('should return an etag', function() { bundle.finalize({}); - var eTag = crypto.createHash('md5').update(bundle.getSource()).digest('hex'); + var eTag = crypto + .createHash('md5') + .update(bundle.getSource()) + .digest('hex'); expect(bundle.getEtag()).toEqual(eTag); }); }); @@ -305,51 +328,86 @@ describe('Bundle', () => { it('can create a single group', () => { bundle = createBundle([fsLocation('React')]); const {groups} = bundle.getUnbundle(); - expect(groups).toEqual(new Map([ - [idFor('React'), new Set(['ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor))], - ])); + expect(groups).toEqual( + new Map([ + [ + idFor('React'), + new Set(['ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor)), + ], + ]), + ); }); it('can create two groups', () => { bundle = createBundle([fsLocation('ReactFoo'), fsLocation('ReactBar')]); const {groups} = bundle.getUnbundle(); - expect(groups).toEqual(new Map([ - [idFor('ReactFoo'), new Set([idFor('invariant')])], - [idFor('ReactBar'), new Set([idFor('cx')])], - ])); + expect(groups).toEqual( + new Map([ + [idFor('ReactFoo'), new Set([idFor('invariant')])], + [idFor('ReactBar'), new Set([idFor('cx')])], + ]), + ); }); it('can handle circular dependencies', () => { bundle = createBundle([fsLocation('OtherFramework')]); const {groups} = bundle.getUnbundle(); - expect(groups).toEqual(new Map([[ - idFor('OtherFramework'), - new Set(['OtherFrameworkFoo', 'invariant', 'OtherFrameworkBar', 'crc32'].map(idFor)), - ]])); + expect(groups).toEqual( + new Map([ + [ + idFor('OtherFramework'), + new Set( + [ + 'OtherFrameworkFoo', + 'invariant', + 'OtherFrameworkBar', + 'crc32', + ].map(idFor), + ), + ], + ]), + ); }); it('omits modules that are contained by more than one group', () => { - bundle = createBundle([fsLocation('React'), fsLocation('OtherFramework')]); + bundle = createBundle([ + fsLocation('React'), + fsLocation('OtherFramework'), + ]); expect(() => { const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars - }).toThrow(new Error(`Module ${fsLocation('invariant')} belongs to groups ${fsLocation('React')}` + - `, and ${fsLocation('OtherFramework')}. Removing it from all groups.`)); + }).toThrow( + new Error( + `Module ${fsLocation('invariant')} belongs to groups ${fsLocation('React')}` + + `, and ${fsLocation('OtherFramework')}. Removing it from all groups.`, + ), + ); }); it('ignores missing dependencies', () => { bundle = createBundle([fsLocation('Product1')]); const {groups} = bundle.getUnbundle(); - expect(groups).toEqual(new Map([[ - idFor('Product1'), - new Set(['React', 'ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor)), - ]])); + expect(groups).toEqual( + new Map([ + [ + idFor('Product1'), + new Set( + ['React', 'ReactFoo', 'invariant', 'ReactBar', 'cx'].map(idFor), + ), + ], + ]), + ); }); it('throws for group roots that do not exist', () => { bundle = createBundle([fsLocation('DoesNotExist')]); expect(() => { const {groups} = bundle.getUnbundle(); //eslint-disable-line no-unused-vars - }).toThrow(new Error(`Group root ${fsLocation('DoesNotExist')} is not part of the bundle`)); + }).toThrow( + new Error( + `Group root ${fsLocation('DoesNotExist')} is not part of the bundle`, + ), + ); }); function idFor(name) { @@ -397,7 +455,17 @@ function resolverFor(code, map) { }; } -function addModule({bundle, code, sourceCode, sourcePath, map, virtual, polyfill, meta, id = ''}) { +function addModule({ + bundle, + code, + sourceCode, + sourcePath, + map, + virtual, + polyfill, + meta, + id = '', +}) { return bundle.addModule( resolverFor(code, map), null, diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js index 7c202be9..5503448e 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js @@ -5,6 +5,8 @@ * 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 * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ 'use strict'; @@ -46,10 +48,12 @@ describe('transforming JS modules:', () => { it('passes through file name and code', done => { transformModule(sourceCode, options(), (error, result) => { expect(result.type).toBe('code'); - expect(result.details).toEqual(expect.objectContaining({ - code: sourceCode, - file: filename, - })); + expect(result.details).toEqual( + expect.objectContaining({ + code: sourceCode, + file: filename, + }), + ); done(); }); }); @@ -73,11 +77,17 @@ describe('transforming JS modules:', () => { }); it('sets `type` to `"script"` if the input is a polyfill', done => { - transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => { - expect(result.type).toBe('code'); - expect(result.details).toEqual(expect.objectContaining({type: 'script'})); - done(); - }); + transformModule( + sourceCode, + {...options(), polyfill: true}, + (error, result) => { + expect(result.type).toBe('code'); + expect(result.details).toEqual( + expect.objectContaining({type: 'script'}), + ); + done(); + }, + ); }); const defaults = { @@ -89,24 +99,25 @@ describe('transforming JS modules:', () => { projectRoot: '', }; - it('calls the passed-in transform function with code, file name, and options ' + - 'for all passed in variants', + it( + 'calls the passed-in transform function with code, file name, and options ' + + 'for all passed in variants', done => { const variants = {dev: {dev: true}, prod: {dev: false}}; transformModule(sourceCode, options(variants), () => { expect(transformer.transform).toBeCalledWith({ - filename, - localPath: filename, - options: {...defaults, ...variants.dev}, - src: sourceCode, - }); + filename, + localPath: filename, + options: {...defaults, ...variants.dev}, + src: sourceCode, + }); expect(transformer.transform).toBeCalledWith({ - filename, - localPath: filename, - options: {...defaults, ...variants.prod}, - src: sourceCode, - }); + filename, + localPath: filename, + options: {...defaults, ...variants.prod}, + src: sourceCode, + }); done(); }); }, @@ -127,24 +138,27 @@ describe('transforming JS modules:', () => { expect(error).toEqual(null); const {code, dependencyMapName} = result.details.transformed.default; - expect(code.replace(/\s+/g, '')) - .toEqual( - `__d(function(global,require,module,exports,${ - dependencyMapName}){${transformedCode}});` - ); + expect(code.replace(/\s+/g, '')).toEqual( + `__d(function(global,require,module,exports,${dependencyMapName}){${transformedCode}});`, + ); done(); }); }); it('wraps the code produced by the transform function into an IIFE for polyfills', done => { - transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => { - expect(error).toEqual(null); + transformModule( + sourceCode, + {...options(), polyfill: true}, + (error, result) => { + expect(error).toEqual(null); - const {code} = result.details.transformed.default; - expect(code.replace(/\s+/g, '')) - .toEqual(`(function(global){${transformedCode}})(this);`); - done(); - }); + const {code} = result.details.transformed.default; + expect(code.replace(/\s+/g, '')).toEqual( + `(function(global){${transformedCode}})(this);`, + ); + done(); + }, + ); }); it('creates source maps', done => { @@ -152,8 +166,9 @@ describe('transforming JS modules:', () => { const {code, map} = result.details.transformed.default; const column = code.indexOf('code'); const consumer = new SourceMapConsumer(map); - expect(consumer.originalPositionFor({line: 1, column})) - .toEqual(expect.objectContaining({line: 1, column: sourceCode.indexOf('code')})); + expect(consumer.originalPositionFor({line: 1, column})).toEqual( + expect.objectContaining({line: 1, column: sourceCode.indexOf('code')}), + ); done(); }); }); @@ -166,8 +181,9 @@ describe('transforming JS modules:', () => { transformer.transform.stub.returns(transformResult(body)); transformModule(code, options(), (error, result) => { - expect(result.details.transformed.default) - .toEqual(expect.objectContaining({dependencies: [dep1, dep2]})); + expect(result.details.transformed.default).toEqual( + expect.objectContaining({dependencies: [dep1, dep2]}), + ); done(); }); }); @@ -176,22 +192,18 @@ describe('transforming JS modules:', () => { const variants = {dev: {dev: true}, prod: {dev: false}}; transformer.transform.stub .withArgs(filename, sourceCode, variants.dev) - .returns(transformResult(bodyAst)) + .returns(transformResult(bodyAst)) .withArgs(filename, sourceCode, variants.prod) - .returns(transformResult([])); + .returns(transformResult([])); transformModule(sourceCode, options(variants), (error, result) => { const {dev, prod} = result.details.transformed; - expect(dev.code.replace(/\s+/g, '')) - .toEqual( - `__d(function(global,require,module,exports,${ - dev.dependencyMapName}){arbitrary(code);});` - ); - expect(prod.code.replace(/\s+/g, '')) - .toEqual( - `__d(function(global,require,module,exports,${ - prod.dependencyMapName}){arbitrary(code);});` - ); + expect(dev.code.replace(/\s+/g, '')).toEqual( + `__d(function(global,require,module,exports,${dev.dependencyMapName}){arbitrary(code);});`, + ); + expect(prod.code.replace(/\s+/g, '')).toEqual( + `__d(function(global,require,module,exports,${prod.dependencyMapName}){arbitrary(code);});`, + ); done(); }); }); @@ -199,23 +211,31 @@ describe('transforming JS modules:', () => { it('prefixes JSON files with `module.exports = `', done => { const json = '{"foo":"bar"}'; - transformModule(json, {...options(), filename: 'some.json'}, (error, result) => { - const {code} = result.details.transformed.default; - expect(code.replace(/\s+/g, '')) - .toEqual( + transformModule( + json, + {...options(), filename: 'some.json'}, + (error, result) => { + const {code} = result.details.transformed.default; + expect(code.replace(/\s+/g, '')).toEqual( '__d(function(global,require,module,exports){' + - `module.exports=${json}});` + `module.exports=${json}});`, ); - done(); - }); + done(); + }, + ); }); it('does not create source maps for JSON files', done => { - transformModule('{}', {...options(), filename: 'some.json'}, (error, result) => { - expect(result.details.transformed.default) - .toEqual(expect.objectContaining({map: null})); - done(); - }); + transformModule( + '{}', + {...options(), filename: 'some.json'}, + (error, result) => { + expect(result.details.transformed.default).toEqual( + expect.objectContaining({map: null}), + ); + done(); + }, + ); }); it('adds package data for `package.json` files', done => { diff --git a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js index 15b2fceb..b5704e4f 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @flow + * @format */ 'use strict'; @@ -70,12 +71,13 @@ function transformModule( const {filename, transformer, variants = defaultVariants} = options; const tasks = {}; Object.keys(variants).forEach(name => { - tasks[name] = asyncify(() => transformer.transform({ + tasks[name] = asyncify(() => + transformer.transform({ filename, localPath: filename, options: {...defaultTransformOptions, ...variants[name]}, src: code, - }) + }), ); }); @@ -89,7 +91,12 @@ function transformModule( //$FlowIssue #14545724 Object.entries(results).forEach(([key, value]: [*, TransformFnResult]) => { - transformed[key] = makeResult(value.ast, filename, code, options.polyfill); + transformed[key] = makeResult( + value.ast, + filename, + code, + options.polyfill, + ); }); const annotations = docblock.parseAsObject(docblock.extract(code)); @@ -112,10 +119,7 @@ function transformModule( function transformJSON(json, options, callback) { const value = JSON.parse(json); const {filename} = options; - const code = - `__d(function(${JsFileWrapping.MODULE_FACTORY_PARAMETERS.join(', ')}) { module.exports = \n${ - json - }\n});`; + const code = `__d(function(${JsFileWrapping.MODULE_FACTORY_PARAMETERS.join(', ')}) { module.exports = \n${json}\n});`; const moduleData = { code, @@ -124,9 +128,9 @@ function transformJSON(json, options, callback) { }; const transformed = {}; - Object - .keys(options.variants || defaultVariants) - .forEach(key => (transformed[key] = moduleData)); + Object.keys(options.variants || defaultVariants).forEach( + key => (transformed[key] = moduleData), + ); const result: TransformedCodeFile = { assetContent: null, diff --git a/packages/metro-bundler/src/Server/symbolicate/__tests__/worker-test.js b/packages/metro-bundler/src/Server/symbolicate/__tests__/worker-test.js index 62dee94e..efa77038 100644 --- a/packages/metro-bundler/src/Server/symbolicate/__tests__/worker-test.js +++ b/packages/metro-bundler/src/Server/symbolicate/__tests__/worker-test.js @@ -5,6 +5,8 @@ * 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 * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ 'use strict'; @@ -35,16 +37,15 @@ it('symbolicates stack frames', () => { ]; const stack = mappings.map(m => m.to); - const maps = - Object.entries(groupBy(mappings, m => m.from.file)) - .map(([file, ms]) => [file, sourceMap(file, ms)]); + const maps = Object.entries( + groupBy(mappings, m => m.from.file), + ).map(([file, ms]) => [file, sourceMap(file, ms)]); - return symbolicate(connection, makeData(stack, maps)) - .then(() => - expect(connection.end).toBeCalledWith( - JSON.stringify({result: mappings.map(m => m.to)}) - ) - ); + return symbolicate(connection, makeData(stack, maps)).then(() => + expect(connection.end).toBeCalledWith( + JSON.stringify({result: mappings.map(m => m.to)}), + ), + ); }); it('ignores stack frames without corresponding map', () => { @@ -54,12 +55,12 @@ it('ignores stack frames without corresponding map', () => { column: 456, }; - return symbolicate(connection, makeData([frame], [['other.js', emptyMap()]])) - .then(() => - expect(connection.end).toBeCalledWith( - JSON.stringify({result: [frame]}) - ) - ); + return symbolicate( + connection, + makeData([frame], [['other.js', emptyMap()]]), + ).then(() => + expect(connection.end).toBeCalledWith(JSON.stringify({result: [frame]})), + ); }); it('ignores `/debuggerWorker.js` stack frames', () => { @@ -69,12 +70,9 @@ it('ignores `/debuggerWorker.js` stack frames', () => { column: 456, }; - return symbolicate(connection, makeData([frame])) - .then(() => - expect(connection.end).toBeCalledWith( - JSON.stringify({result: [frame]}) - ) - ); + return symbolicate(connection, makeData([frame])).then(() => + expect(connection.end).toBeCalledWith(JSON.stringify({result: [frame]})), + ); }); function makeData(stack, maps = []) { @@ -85,7 +83,8 @@ function sourceMap(file, mappings) { const g = new SourceMapGenerator(); g.startFile(file, null); mappings.forEach(({from, to}) => - g.addSourceMapping(to.lineNumber, to.column, from.lineNumber, from.column)); + g.addSourceMapping(to.lineNumber, to.column, from.lineNumber, from.column), + ); return g.toMap(); } diff --git a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js index b567bdf0..0a3938b6 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js @@ -5,6 +5,8 @@ * 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 * of patent rights can be found in the PATENTS file in the same directory. + * + * @format */ 'use strict'; @@ -15,14 +17,16 @@ jest .mock('../../Logger') .mock('../../lib/TransformCache') // It's noticeably faster to prevent running watchman from FileWatcher. - .mock('child_process', () => ({})) - ; + .mock('child_process', () => ({})); // This doesn't have state, and it's huge (Babel) so it's much faster to // require it only once. The variable name is prefixed with "mock" as an escape-hatch // for babel-plugin-jest-hoist. const mockExtractDependencies = require('../../JSTransformer/worker/extract-dependencies'); -jest.mock('../../JSTransformer/worker/extract-dependencies', () => mockExtractDependencies); +jest.mock( + '../../JSTransformer/worker/extract-dependencies', + () => mockExtractDependencies, +); jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; @@ -39,28 +43,40 @@ describe('DependencyGraph', function() { let defaults; let emptyTransformOptions; - function getOrderedDependenciesAsJSON(dgraphPromise, entryPath, platform, recursive = true) { + function getOrderedDependenciesAsJSON( + dgraphPromise, + entryPath, + platform, + recursive = true, + ) { return Promise.resolve(dgraphPromise) - .then(dgraph => dgraph.getDependencies({ - entryPath, - options: emptyTransformOptions, - platform, - recursive, - })) + .then(dgraph => + dgraph.getDependencies({ + entryPath, + options: emptyTransformOptions, + platform, + recursive, + }), + ) .then(response => response.finalize()) - .then(({dependencies}) => Promise.all(dependencies.map(dep => Promise.all([ - dep.getName(), - dep.getDependencies(), - ]).then(([name, moduleDependencies]) => ({ - path: dep.path, - isJSON: dep.isJSON(), - isAsset: dep.isAsset(), - isPolyfill: dep.isPolyfill(), - resolution: dep.resolution, - id: name, - dependencies: moduleDependencies, - }))) - )); + .then(({dependencies}) => + Promise.all( + dependencies.map(dep => + Promise.all([ + dep.getName(), + dep.getDependencies(), + ]).then(([name, moduleDependencies]) => ({ + path: dep.path, + isJSON: dep.isJSON(), + isAsset: dep.isAsset(), + isPolyfill: dep.isPolyfill(), + resolution: dep.resolution, + id: name, + dependencies: moduleDependencies, + })), + ), + ), + ); } beforeEach(function() { @@ -73,11 +89,7 @@ describe('DependencyGraph', function() { defaults = { assetExts: ['png', 'jpg'], forceNodeFilesystemAPI: true, - providesModuleNodeModules: [ - 'haste-fbjs', - 'react-haste', - 'react-native', - ], + providesModuleNodeModules: ['haste-fbjs', 'react-haste', 'react-native'], platforms: new Set(['ios', 'android']), useWatchman: false, ignoreFilePath: () => false, @@ -117,24 +129,17 @@ describe('DependencyGraph', function() { it('should get dependencies', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("a")', ].join('\n'), - 'a.js': [ - '/**', - ' * @providesModule a', - ' */', - 'require("b")', - ].join('\n'), - 'b.js': [ - '/**', - ' * @providesModule b', - ' */', - ].join('\n'), + 'a.js': ['/**', ' * @providesModule a', ' */', 'require("b")'].join( + '\n', + ), + 'b.js': ['/**', ' * @providesModule b', ' */'].join('\n'), }, }); @@ -142,52 +147,50 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'a', - path: '/root/a.js', - dependencies: ['b'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'b', - path: '/root/b.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'a', + path: '/root/a.js', + dependencies: ['b'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'b', + path: '/root/b.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); }); }); it('should resolve relative entry path', function() { var root = '/root'; setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - ].join('\n'), + root: { + 'index.js': ['/**', ' * @providesModule index', ' */'].join('\n'), }, }); @@ -195,43 +198,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, 'index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON(dgraph, 'index.js').then(function( + deps, + ) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should get shallow dependencies', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("a")', ].join('\n'), - 'a.js': [ - '/**', - ' * @providesModule a', - ' */', - 'require("b")', - ].join('\n'), - 'b.js': [ - '/**', - ' * @providesModule b', - ' */', - ].join('\n'), + 'a.js': ['/**', ' * @providesModule a', ' */', 'require("b")'].join( + '\n', + ), + 'b.js': ['/**', ' * @providesModule b', ' */'].join('\n'), }, }); @@ -239,51 +236,47 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js', null, false).then(deps => { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: '/root/a.js', - dependencies: ['b'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + null, + false, + ).then(deps => { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: '/root/a.js', + dependencies: ['b'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should get dependencies with the correct extensions', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("a")', ].join('\n'), - 'a.js': [ - '/**', - ' * @providesModule a', - ' */', - ].join('\n'), - 'a.js.orig': [ - '/**', - ' * @providesModule a', - ' */', - ].join('\n'), + 'a.js': ['/**', ' * @providesModule a', ' */'].join('\n'), + 'a.js.orig': ['/**', ' * @providesModule a', ' */'].join('\n'), }, }); @@ -291,35 +284,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: '/root/a.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: '/root/a.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should get json dependencies', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'package.json': JSON.stringify({ name: 'package', }), @@ -339,44 +334,46 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['./a.json', './b'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'package/a.json', - isJSON: true, - path: '/root/a.json', - dependencies: [], - isAsset: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'package/b.json', - isJSON: true, - path: '/root/b.json', - dependencies: [], - isAsset: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./a.json', './b'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'package/a.json', + isJSON: true, + path: '/root/a.json', + dependencies: [], + isAsset: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'package/b.json', + isJSON: true, + path: '/root/b.json', + dependencies: [], + isAsset: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should get package json as a dep', () => { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'package.json': JSON.stringify({ name: 'package', }), @@ -393,42 +390,44 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['./package.json'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'package/package.json', - isJSON: true, - path: '/root/package.json', - dependencies: [], - isAsset: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./package.json'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'package/package.json', + isJSON: true, + path: '/root/package.json', + dependencies: [], + isAsset: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should get dependencies with relative assets', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("./imgs/a.png")', ].join('\n'), - 'imgs': { + imgs: { 'a.png': '', }, 'package.json': JSON.stringify({ @@ -441,35 +440,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['./imgs/a.png'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'rootPackage/imgs/a.png', - path: '/root/imgs/a.png', - dependencies: [], - isAsset: true, - resolution: 1, - isJSON: false, - isPolyfill: false, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./imgs/a.png'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rootPackage/imgs/a.png', + path: '/root/imgs/a.png', + dependencies: [], + isAsset: true, + resolution: 1, + isJSON: false, + isPolyfill: false, + }, + ]); }); }); it('should get dependencies with assets and resolution', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -478,7 +479,7 @@ describe('DependencyGraph', function() { 'require("./imgs/b.png");', 'require("./imgs/c.png");', ].join('\n'), - 'imgs': { + imgs: { 'a@1.5x.png': '', 'b@.7x.png': '', 'c.png': '', @@ -494,57 +495,55 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: [ - './imgs/a.png', - './imgs/b.png', - './imgs/c.png', - ], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'rootPackage/imgs/a.png', - path: '/root/imgs/a@1.5x.png', - resolution: 1.5, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - { - id: 'rootPackage/imgs/b.png', - path: '/root/imgs/b@.7x.png', - resolution: 0.7, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - { - id: 'rootPackage/imgs/c.png', - path: '/root/imgs/c.png', - resolution: 1, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./imgs/a.png', './imgs/b.png', './imgs/c.png'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rootPackage/imgs/a.png', + path: '/root/imgs/a@1.5x.png', + resolution: 1.5, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + { + id: 'rootPackage/imgs/b.png', + path: '/root/imgs/b@.7x.png', + resolution: 0.7, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + { + id: 'rootPackage/imgs/c.png', + path: '/root/imgs/c.png', + resolution: 1, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + ]); }); }); it('should respect platform extension in assets', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -553,7 +552,7 @@ describe('DependencyGraph', function() { 'require("./imgs/b.png");', 'require("./imgs/c.png");', ].join('\n'), - 'imgs': { + imgs: { 'a@1.5x.ios.png': '', 'b@.7x.ios.png': '', 'c.ios.png': '', @@ -570,57 +569,56 @@ describe('DependencyGraph', function() { roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js', 'ios').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: [ - './imgs/a.png', - './imgs/b.png', - './imgs/c.png', - ], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'rootPackage/imgs/a.png', - path: '/root/imgs/a@1.5x.ios.png', - resolution: 1.5, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - { - id: 'rootPackage/imgs/b.png', - path: '/root/imgs/b@.7x.ios.png', - resolution: 0.7, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - { - id: 'rootPackage/imgs/c.png', - path: '/root/imgs/c.ios.png', - resolution: 1, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + 'ios', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./imgs/a.png', './imgs/b.png', './imgs/c.png'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rootPackage/imgs/a.png', + path: '/root/imgs/a@1.5x.ios.png', + resolution: 1.5, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + { + id: 'rootPackage/imgs/b.png', + path: '/root/imgs/b@.7x.ios.png', + resolution: 0.7, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + { + id: 'rootPackage/imgs/c.png', + path: '/root/imgs/c.ios.png', + resolution: 1, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + ]); }); }); it('should get recursive dependencies', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -640,42 +638,44 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: '/root/a.js', - dependencies: ['index'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: '/root/a.js', + dependencies: ['index'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with packages', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -689,42 +689,44 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/main.js', - path: '/root/aPackage/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/main.js', + path: '/root/aPackage/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with packages with a trailing slash', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage/")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -738,35 +740,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage/'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/main.js', - path: '/root/aPackage/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage/'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/main.js', + path: '/root/aPackage/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with packages with a dot in the name', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -795,46 +799,48 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['sha.js', 'x.y.z'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'sha.js/main.js', - path: '/root/sha.js/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'x.y.z/main.js', - path: '/root/x.y.z/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['sha.js', 'x.y.z'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'sha.js/main.js', + path: '/root/sha.js/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'x.y.z/main.js', + path: '/root/x.y.z/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should default main package to index.js', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': 'require("aPackage")', - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', }), @@ -847,45 +853,45 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/index.js', - path: '/root/aPackage/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/index.js', + path: '/root/aPackage/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should resolve using alternative ids', () => { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': 'require("aPackage")', - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', }), - 'index.js': [ - '/**', - ' * @providesModule EpicModule', - ' */', - ].join('\n'), + 'index.js': ['/**', ' * @providesModule EpicModule', ' */'].join( + '\n', + ), }, }, }); @@ -894,37 +900,39 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'EpicModule', - path: '/root/aPackage/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'EpicModule', + path: '/root/aPackage/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should default use index.js if main is a dir', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': 'require("aPackage")', - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'lib', @@ -940,35 +948,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/lib/index.js', - path: '/root/aPackage/lib/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/lib/index.js', + path: '/root/aPackage/lib/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should resolve require to index if it is a dir', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'package.json': JSON.stringify({ name: 'test', }), @@ -983,42 +993,44 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'test/index.js', - path: '/root/index.js', - dependencies: ['./lib/'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'test/lib/index.js', - path: '/root/lib/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'test/index.js', + path: '/root/index.js', + dependencies: ['./lib/'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'test/lib/index.js', + path: '/root/lib/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should resolve require to main if it is a dir w/ a package.json', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'package.json': JSON.stringify({ name: 'test', }), 'index.js': 'require("./lib/")', lib: { 'package.json': JSON.stringify({ - 'main': 'main.js', + main: 'main.js', }), 'index.js': 'lol', 'main.js': 'lol', @@ -1030,41 +1042,39 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'test/index.js', - path: '/root/index.js', - dependencies: ['./lib/'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/lib/main.js', - path: '/root/lib/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'test/index.js', + path: '/root/index.js', + dependencies: ['./lib/'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/lib/main.js', + path: '/root/lib/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should ignore malformed packages', function() { var root = '/root'; setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - ].join('\n'), - 'aPackage': { + root: { + 'index.js': ['/**', ' * @providesModule index', ' */'].join('\n'), + aPackage: { 'package.json': 'lol', 'main.js': 'lol', }, @@ -1075,19 +1085,21 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -1095,17 +1107,9 @@ describe('DependencyGraph', function() { const root = '/root'; console.warn = jest.fn(); setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - ].join('\n'), - 'b.js': [ - '/**', - ' * @providesModule index', - ' */', - ].join('\n'), + root: { + 'index.js': ['/**', ' * @providesModule index', ' */'].join('\n'), + 'b.js': ['/**', ' * @providesModule index', ' */'].join('\n'), }, }); @@ -1117,10 +1121,10 @@ describe('DependencyGraph', function() { return dgraph.catch(err => { expect(err.message).toEqual( `Failed to build DependencyGraph: @providesModule naming collision:\n` + - ` Duplicate module name: index\n` + - ` Paths: /root/b.js collides with /root/index.js\n\n` + - 'This error is caused by a @providesModule declaration ' + - 'with the same name across two different files.' + ` Duplicate module name: index\n` + + ` Paths: /root/b.js collides with /root/index.js\n\n` + + 'This error is caused by a @providesModule declaration ' + + 'with the same name across two different files.', ); expect(err.type).toEqual('DependencyGraphError'); expect(console.warn).toBeCalled(); @@ -1130,7 +1134,7 @@ describe('DependencyGraph', function() { it('throws when a module is missing', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -1144,30 +1148,31 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').catch( - error => { - expect(error.type).toEqual('UnableToResolveError'); - } - ); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).catch(error => { + expect(error.type).toEqual('UnableToResolveError'); + }); }); it('should work with packages with subdirs', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage/subdir/lolynot")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', }), 'main.js': 'lol', - 'subdir': { + subdir: { 'lolynot.js': 'lolynot', }, }, @@ -1178,50 +1183,52 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage/subdir/lolynot'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'aPackage/subdir/lolynot.js', - path: '/root/aPackage/subdir/lolynot.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage/subdir/lolynot'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'aPackage/subdir/lolynot.js', + path: '/root/aPackage/subdir/lolynot.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); }); }); it('should work with relative modules in packages', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', }), 'main.js': 'require("./subdir/lolynot")', - 'subdir': { + subdir: { 'lolynot.js': 'require("../other")', }, 'other.js': '/* some code */', @@ -1233,50 +1240,52 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'aPackage/main.js', - path: '/root/aPackage/main.js', - dependencies: ['./subdir/lolynot'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'aPackage/subdir/lolynot.js', - path: '/root/aPackage/subdir/lolynot.js', - dependencies: ['../other'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'aPackage/other.js', - path: '/root/aPackage/other.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'aPackage/main.js', + path: '/root/aPackage/main.js', + dependencies: ['./subdir/lolynot'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'aPackage/subdir/lolynot.js', + path: '/root/aPackage/subdir/lolynot.js', + dependencies: ['../other'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'aPackage/other.js', + path: '/root/aPackage/other.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); }); }); @@ -1293,35 +1302,44 @@ describe('DependencyGraph', function() { } function testBrowserField(fieldName) { - it('should support simple browser field in packages ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - main: 'main.js', - browser: 'client.js', - }, fieldName)), - 'main.js': 'some other code', - 'client.js': '/* some code */', + it( + 'should support simple browser field in packages ("' + fieldName + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + main: 'main.js', + browser: 'client.js', + }, + fieldName, + ), + ), + 'main.js': 'some other code', + 'client.js': '/* some code */', + }, }, - }, - }); + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ { id: 'index', path: '/root/index.js', @@ -1343,38 +1361,50 @@ describe('DependencyGraph', function() { resolveDependency: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser field in packages w/o .js ext ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - main: 'main.js', - browser: 'client', - }, fieldName)), - 'main.js': 'some other code', - 'client.js': '/* some code */', + it( + 'should support browser field in packages w/o .js ext ("' + + fieldName + + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + main: 'main.js', + browser: 'client', + }, + fieldName, + ), + ), + 'main.js': 'some other code', + 'client.js': '/* some code */', + }, }, - }, - }); + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ { id: 'index', path: '/root/index.js', @@ -1394,41 +1424,53 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support mapping main in browser field json ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - main: './main.js', - browser: { - './main.js': './client.js', - }, - }, fieldName)), - 'main.js': 'some other code', - 'client.js': '/* some code */', + it( + 'should support mapping main in browser field json ("' + + fieldName + + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + main: './main.js', + browser: { + './main.js': './client.js', + }, + }, + fieldName, + ), + ), + 'main.js': 'some other code', + 'client.js': '/* some code */', + }, }, - }, - }); + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - assetExts: ['png', 'jpg'], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + assetExts: ['png', 'jpg'], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ { id: 'index', path: '/root/index.js', @@ -1438,7 +1480,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/client.js', + { + id: 'aPackage/client.js', path: '/root/aPackage/client.js', dependencies: [], isAsset: false, @@ -1448,41 +1491,53 @@ describe('DependencyGraph', function() { resolveDependency: undefined, }, ]); - }); - }); + }); + }, + ); - it('should work do correct browser mapping w/o js ext ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - main: './main.js', - browser: { - './main': './client.js', - }, - }, fieldName)), - 'main.js': 'some other code', - 'client.js': '/* some code */', + it( + 'should work do correct browser mapping w/o js ext ("' + + fieldName + + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + main: './main.js', + browser: { + './main': './client.js', + }, + }, + fieldName, + ), + ), + 'main.js': 'some other code', + 'client.js': '/* some code */', + }, }, - }, - }); + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - assetExts: ['png', 'jpg'], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + assetExts: ['png', 'jpg'], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ { id: 'index', path: '/root/index.js', @@ -1504,54 +1559,65 @@ describe('DependencyGraph', function() { resolveDependency: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser mapping of files ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - main: './main.js', - browser: { - './main': './client.js', - './node.js': './not-node.js', - './not-browser': './browser.js', - './dir/server.js': './dir/client', - './hello.js': './bye.js', + it( + 'should support browser mapping of files ("' + fieldName + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + main: './main.js', + browser: { + './main': './client.js', + './node.js': './not-node.js', + './not-browser': './browser.js', + './dir/server.js': './dir/client', + './hello.js': './bye.js', + }, + }, + fieldName, + ), + ), + 'main.js': '/* some other code */', + 'client.js': 'require("./node")\nrequire("./dir/server.js")', + 'not-node.js': 'require("./not-browser")', + 'not-browser.js': 'require("./dir/server")', + 'browser.js': '/* some browser code */', + dir: { + 'server.js': '/* some node code */', + 'client.js': 'require("../hello")', }, - }, fieldName)), - 'main.js': '/* some other code */', - 'client.js': 'require("./node")\nrequire("./dir/server.js")', - 'not-node.js': 'require("./not-browser")', - 'not-browser.js': 'require("./dir/server")', - 'browser.js': '/* some browser code */', - 'dir': { - 'server.js': '/* some node code */', - 'client.js': 'require("../hello")', + 'hello.js': '/* hello */', + 'bye.js': '/* bye */', }, - 'hello.js': '/* hello */', - 'bye.js': '/* bye */', }, - }, - }); + }); - const dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', + const dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', path: '/root/index.js', dependencies: ['aPackage'], isAsset: false, @@ -1559,7 +1625,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/client.js', + { + id: 'aPackage/client.js', path: '/root/aPackage/client.js', dependencies: ['./node', './dir/server.js'], isAsset: false, @@ -1567,7 +1634,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/not-node.js', + { + id: 'aPackage/not-node.js', path: '/root/aPackage/not-node.js', dependencies: ['./not-browser'], isAsset: false, @@ -1575,7 +1643,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/browser.js', + { + id: 'aPackage/browser.js', path: '/root/aPackage/browser.js', dependencies: [], isAsset: false, @@ -1602,51 +1671,62 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser mapping for packages ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - browser: { - 'node-package': 'browser-package', + it( + 'should support browser mapping for packages ("' + fieldName + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + browser: { + 'node-package': 'browser-package', + }, + }, + fieldName, + ), + ), + 'index.js': 'require("node-package")', + 'node-package': { + 'package.json': JSON.stringify({ + name: 'node-package', + }), + 'index.js': '/* some node code */', + }, + 'browser-package': { + 'package.json': JSON.stringify({ + name: 'browser-package', + }), + 'index.js': '/* some browser code */', }, - }, fieldName)), - 'index.js': 'require("node-package")', - 'node-package': { - 'package.json': JSON.stringify({ - 'name': 'node-package', - }), - 'index.js': '/* some node code */', - }, - 'browser-package': { - 'package.json': JSON.stringify({ - 'name': 'browser-package', - }), - 'index.js': '/* some browser code */', }, }, - }, - }); + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', path: '/root/index.js', dependencies: ['aPackage'], isAsset: false, @@ -1654,7 +1734,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/index.js', + { + id: 'aPackage/index.js', path: '/root/aPackage/index.js', dependencies: ['node-package'], isAsset: false, @@ -1662,7 +1743,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'browser-package/index.js', + { + id: 'browser-package/index.js', path: '/root/aPackage/browser-package/index.js', dependencies: [], isAsset: false, @@ -1671,49 +1753,62 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser mapping of a package to a file ("' + fieldName + '")', () => { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - browser: { - 'node-package': './dir/browser.js', + it( + 'should support browser mapping of a package to a file ("' + + fieldName + + '")', + () => { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + browser: { + 'node-package': './dir/browser.js', + }, + }, + fieldName, + ), + ), + 'index.js': 'require("./dir/ooga")', + dir: { + 'ooga.js': 'require("node-package")', + 'browser.js': '/* some browser code */', + }, + 'node-package': { + 'package.json': JSON.stringify({ + name: 'node-package', + }), + 'index.js': '/* some node code */', }, - }, fieldName)), - 'index.js': 'require("./dir/ooga")', - 'dir': { - 'ooga.js': 'require("node-package")', - 'browser.js': '/* some browser code */', - }, - 'node-package': { - 'package.json': JSON.stringify({ - 'name': 'node-package', - }), - 'index.js': '/* some node code */', }, }, - }, - }); + }); - const dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', + const dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', path: '/root/index.js', dependencies: ['aPackage'], isAsset: false, @@ -1721,7 +1816,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/index.js', + { + id: 'aPackage/index.js', path: '/root/aPackage/index.js', dependencies: ['./dir/ooga'], isAsset: false, @@ -1729,7 +1825,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/dir/ooga.js', + { + id: 'aPackage/dir/ooga.js', path: '/root/aPackage/dir/ooga.js', dependencies: ['node-package'], isAsset: false, @@ -1737,7 +1834,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/dir/browser.js', + { + id: 'aPackage/dir/browser.js', path: '/root/aPackage/dir/browser.js', dependencies: [], isAsset: false, @@ -1746,51 +1844,62 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser mapping for packages ("' + fieldName + '")', function() { - var root = '/root'; - setMockFileSystem({ - 'root': { - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - browser: { - 'node-package': 'browser-package', + it( + 'should support browser mapping for packages ("' + fieldName + '")', + function() { + var root = '/root'; + setMockFileSystem({ + root: { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + browser: { + 'node-package': 'browser-package', + }, + }, + fieldName, + ), + ), + 'index.js': 'require("node-package")', + 'node-package': { + 'package.json': JSON.stringify({ + name: 'node-package', + }), + 'index.js': '/* some node code */', + }, + 'browser-package': { + 'package.json': JSON.stringify({ + name: 'browser-package', + }), + 'index.js': '/* some browser code */', }, - }, fieldName)), - 'index.js': 'require("node-package")', - 'node-package': { - 'package.json': JSON.stringify({ - 'name': 'node-package', - }), - 'index.js': '/* some node code */', - }, - 'browser-package': { - 'package.json': JSON.stringify({ - 'name': 'browser-package', - }), - 'index.js': '/* some browser code */', }, }, - }, - }); + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', path: '/root/index.js', dependencies: ['aPackage'], isAsset: false, @@ -1798,7 +1907,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/index.js', + { + id: 'aPackage/index.js', path: '/root/aPackage/index.js', dependencies: ['node-package'], isAsset: false, @@ -1806,7 +1916,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'browser-package/index.js', + { + id: 'browser-package/index.js', path: '/root/aPackage/browser-package/index.js', dependencies: [], isAsset: false, @@ -1815,47 +1926,58 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser exclude of a package ("' + fieldName + '")', function() { - ResolutionRequest.EMPTY_MODULE = '/root/emptyModule.js'; - var root = '/root'; - setMockFileSystem({ - 'root': { - 'emptyModule.js': '', - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - browser: { - 'booga': false, + it( + 'should support browser exclude of a package ("' + fieldName + '")', + function() { + ResolutionRequest.EMPTY_MODULE = '/root/emptyModule.js'; + var root = '/root'; + setMockFileSystem({ + root: { + 'emptyModule.js': '', + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + browser: { + booga: false, + }, + }, + fieldName, + ), + ), + 'index.js': 'require("booga")', + booga: { + 'package.json': JSON.stringify({ + name: 'booga', + }), + 'index.js': '/* some node code */', }, - }, fieldName)), - 'index.js': 'require("booga")', - 'booga': { - 'package.json': JSON.stringify({ - 'name': 'booga', - }), - 'index.js': '/* some node code */', }, }, - }, - }); + }); - const dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', + const dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', path: '/root/index.js', dependencies: ['aPackage'], isAsset: false, @@ -1863,7 +1985,8 @@ describe('DependencyGraph', function() { isPolyfill: false, resolution: undefined, }, - {id: 'aPackage/index.js', + { + id: 'aPackage/index.js', path: '/root/aPackage/index.js', dependencies: ['booga'], isAsset: false, @@ -1881,42 +2004,52 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); - it('should support browser exclude of a file ("' + fieldName + '")', function() { - ResolutionRequest.EMPTY_MODULE = '/root/emptyModule.js'; + it( + 'should support browser exclude of a file ("' + fieldName + '")', + function() { + ResolutionRequest.EMPTY_MODULE = '/root/emptyModule.js'; - var root = '/root'; - setMockFileSystem({ - 'root': { - 'emptyModule.js': '', - 'index.js': [ - '/**', - ' * @providesModule index', - ' */', - 'require("aPackage")', - ].join('\n'), - 'aPackage': { - 'package.json': JSON.stringify(replaceBrowserField({ - name: 'aPackage', - browser: { - './booga.js': false, - }, - }, fieldName)), - 'index.js': 'require("./booga")', - 'booga.js': '/* some node code */', + var root = '/root'; + setMockFileSystem({ + root: { + 'emptyModule.js': '', + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + aPackage: { + 'package.json': JSON.stringify( + replaceBrowserField( + { + name: 'aPackage', + browser: { + './booga.js': false, + }, + }, + fieldName, + ), + ), + 'index.js': 'require("./booga")', + 'booga.js': '/* some node code */', + }, }, - }, - }); + }); - const dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ + const dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ { id: 'index', path: '/root/index.js', @@ -1945,21 +2078,22 @@ describe('DependencyGraph', function() { resolution: undefined, }, ]); - }); - }); + }); + }, + ); } it('should fall back to browser mapping from react-native mapping', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', 'react-native': { @@ -1967,16 +2101,16 @@ describe('DependencyGraph', function() { }, }), 'index.js': 'require("node-package")', - 'node_modules': { + node_modules: { 'node-package': { 'package.json': JSON.stringify({ - 'name': 'node-package', + name: 'node-package', }), 'index.js': '/* some node code */', }, 'rn-package': { 'package.json': JSON.stringify({ - 'name': 'rn-package', + name: 'rn-package', browser: { 'nested-package': 'nested-browser-package', }, @@ -1985,7 +2119,7 @@ describe('DependencyGraph', function() { }, 'nested-browser-package': { 'package.json': JSON.stringify({ - 'name': 'nested-browser-package', + name: 'nested-browser-package', }), 'index.js': '/* some code */', }, @@ -1998,42 +2132,48 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'aPackage/index.js', - path: '/root/aPackage/index.js', - dependencies: ['node-package'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'rn-package/index.js', - path: '/root/aPackage/node_modules/rn-package/index.js', - dependencies: ['nested-package'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'nested-browser-package/index.js', - path: '/root/aPackage/node_modules/nested-browser-package/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/index.js', + path: '/root/aPackage/index.js', + dependencies: ['node-package'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rn-package/index.js', + path: '/root/aPackage/node_modules/rn-package/index.js', + dependencies: ['nested-package'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'nested-browser-package/index.js', + path: '/root/aPackage/node_modules/nested-browser-package/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -2050,42 +2190,44 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['/root/apple.js'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/apple.js', - path: '/root/apple.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['/root/apple.js'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/apple.js', + path: '/root/apple.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should merge browser mapping with react-native mapping', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', 'react-native': { @@ -2094,61 +2236,60 @@ describe('DependencyGraph', function() { // should see this: 'node-package-c': 'rn-package-d', }, - 'browser': { + browser: { // should see this: 'node-package-b': 'rn-package-b', // should NOT see this: 'node-package-c': 'rn-package-c', }, }), - 'index.js': - 'require("node-package-a"); require("node-package-b"); require("node-package-c");', - 'node_modules': { + 'index.js': 'require("node-package-a"); require("node-package-b"); require("node-package-c");', + node_modules: { 'node-package-a': { 'package.json': JSON.stringify({ - 'name': 'node-package-a', + name: 'node-package-a', }), 'index.js': '/* some node code */', }, 'node-package-b': { 'package.json': JSON.stringify({ - 'name': 'node-package-b', + name: 'node-package-b', }), 'index.js': '/* some node code */', }, 'node-package-c': { 'package.json': JSON.stringify({ - 'name': 'node-package-c', + name: 'node-package-c', }), 'index.js': '/* some node code */', }, 'node-package-d': { 'package.json': JSON.stringify({ - 'name': 'node-package-d', + name: 'node-package-d', }), 'index.js': '/* some node code */', }, 'rn-package-a': { 'package.json': JSON.stringify({ - 'name': 'rn-package-a', + name: 'rn-package-a', }), 'index.js': '/* some rn code */', }, 'rn-package-b': { 'package.json': JSON.stringify({ - 'name': 'rn-package-b', + name: 'rn-package-b', }), 'index.js': '/* some rn code */', }, 'rn-package-c': { 'package.json': JSON.stringify({ - 'name': 'rn-package-c', + name: 'rn-package-c', }), 'index.js': '/* some rn code */', }, 'rn-package-d': { 'package.json': JSON.stringify({ - 'name': 'rn-package-d', + name: 'rn-package-d', }), 'index.js': '/* some rn code */', }, @@ -2161,50 +2302,61 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - {id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'aPackage/index.js', - path: '/root/aPackage/index.js', - dependencies: ['node-package-a', 'node-package-b', 'node-package-c'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'rn-package-a/index.js', - path: '/root/aPackage/node_modules/rn-package-a/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'rn-package-b/index.js', - path: '/root/aPackage/node_modules/rn-package-b/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - {id: 'rn-package-d/index.js', - path: '/root/aPackage/node_modules/rn-package-d/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/index.js', + path: '/root/aPackage/index.js', + dependencies: [ + 'node-package-a', + 'node-package-b', + 'node-package-c', + ], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rn-package-a/index.js', + path: '/root/aPackage/node_modules/rn-package-a/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rn-package-b/index.js', + path: '/root/aPackage/node_modules/rn-package-b/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rn-package-d/index.js', + path: '/root/aPackage/node_modules/rn-package-d/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -2213,12 +2365,12 @@ describe('DependencyGraph', function() { setMockFileSystem({ [root.slice(1)]: { 'index.js': 'require("./foo")', - 'foo': { + foo: { 'index.js': 'require("bar")', }, 'provides-bar': { 'package.json': '{"main": "lib/bar.js"}', - 'lib': { + lib: { 'bar.js': '', }, }, @@ -2229,89 +2381,90 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], extraNodeModules: { - 'bar': root + '/provides-bar', + bar: root + '/provides-bar', }, }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['./foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/foo/index.js', - path: '/root/foo/index.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/provides-bar/lib/bar.js', - path: '/root/provides-bar/lib/bar.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['./foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/foo/index.js', + path: '/root/foo/index.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/provides-bar/lib/bar.js', + path: '/root/provides-bar/lib/bar.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); - it( - 'should only use `extraNodeModules` after checking all possible filesystem locations', - () => { - const root = '/root'; - setMockFileSystem({ - [root.slice(1)]: { - 'index.js': 'require("bar")', - 'node_modules': {'bar.js': ''}, - 'provides-bar': {'index.js': ''}, - }, - }); + it('should only use `extraNodeModules` after checking all possible filesystem locations', () => { + const root = '/root'; + setMockFileSystem({ + [root.slice(1)]: { + 'index.js': 'require("bar")', + node_modules: {'bar.js': ''}, + 'provides-bar': {'index.js': ''}, + }, + }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - extraNodeModules: { - 'bar': root + '/provides-bar', - }, - }); + var dgraph = DependencyGraph.load({ + ...defaults, + roots: [root], + extraNodeModules: { + bar: root + '/provides-bar', + }, + }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/node_modules/bar.js', - path: '/root/node_modules/bar.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); - }); - } - ); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/node_modules/bar.js', + path: '/root/node_modules/bar.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); + }); + }); it('should be able to resolve paths within `extraNodeModules`', () => { const root = '/root'; @@ -2320,7 +2473,7 @@ describe('DependencyGraph', function() { 'index.js': 'require("bar/lib/foo")', 'provides-bar': { 'package.json': '{}', - 'lib': {'foo.js': ''}, + lib: {'foo.js': ''}, }, }, }); @@ -2329,32 +2482,34 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], extraNodeModules: { - 'bar': root + '/provides-bar', + bar: root + '/provides-bar', }, }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['bar/lib/foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/provides-bar/lib/foo.js', - path: '/root/provides-bar/lib/foo.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['bar/lib/foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/provides-bar/lib/foo.js', + path: '/root/provides-bar/lib/foo.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); }); @@ -2378,24 +2533,17 @@ describe('DependencyGraph', function() { it('should get dependencies', function() { const root = 'C:\\root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("a")', ].join('\n'), - 'a.js': [ - '/**', - ' * @providesModule a', - ' */', - 'require("b")', - ].join('\n'), - 'b.js': [ - '/**', - ' * @providesModule b', - ' */', - ].join('\n'), + 'a.js': ['/**', ' * @providesModule a', ' */', 'require("b")'].join( + '\n', + ), + 'b.js': ['/**', ' * @providesModule b', ' */'].join('\n'), }, }); @@ -2403,47 +2551,49 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, 'C:\\root\\index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'a', - path: 'C:\\root\\a.js', - dependencies: ['b'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'b', - path: 'C:\\root\\b.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + 'C:\\root\\index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'a', + path: 'C:\\root\\a.js', + dependencies: ['b'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'b', + path: 'C:\\root\\b.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); }); }); it('should work with absolute paths', () => { const root = 'C:\\root'; setMockFileSystem({ - 'root': { + root: { 'index.js': 'require("C:/root/apple.js");', 'apple.js': '', }, @@ -2453,35 +2603,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, 'C:\\root\\index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'C:\\root\\index.js', - path: 'C:\\root\\index.js', - dependencies: ['C:/root/apple.js'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'C:\\root\\apple.js', - path: 'C:\\root\\apple.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + 'C:\\root\\index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'C:\\root\\index.js', + path: 'C:\\root\\index.js', + dependencies: ['C:/root/apple.js'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'C:\\root\\apple.js', + path: 'C:\\root\\apple.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should get dependencies with assets and resolution', function() { const root = 'C:\\root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -2490,7 +2642,7 @@ describe('DependencyGraph', function() { 'require("./imgs/b.png");', 'require("./imgs/c.png");', ].join('\n'), - 'imgs': { + imgs: { 'a@1.5x.png': '', 'b@.7x.png': '', 'c.png': '', @@ -2506,50 +2658,48 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, 'C:\\root\\index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: [ - './imgs/a.png', - './imgs/b.png', - './imgs/c.png', - ], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'rootPackage/imgs/a.png', - path: 'C:\\root\\imgs\\a@1.5x.png', - resolution: 1.5, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - { - id: 'rootPackage/imgs/b.png', - path: 'C:\\root\\imgs\\b@.7x.png', - resolution: 0.7, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - { - id: 'rootPackage/imgs/c.png', - path: 'C:\\root\\imgs\\c.png', - resolution: 1, - dependencies: [], - isAsset: true, - isJSON: false, - isPolyfill: false, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + 'C:\\root\\index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['./imgs/a.png', './imgs/b.png', './imgs/c.png'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'rootPackage/imgs/a.png', + path: 'C:\\root\\imgs\\a@1.5x.png', + resolution: 1.5, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + { + id: 'rootPackage/imgs/b.png', + path: 'C:\\root\\imgs\\b@.7x.png', + resolution: 0.7, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + { + id: 'rootPackage/imgs/c.png', + path: 'C:\\root\\imgs\\c.png', + resolution: 1, + dependencies: [], + isAsset: true, + isJSON: false, + isPolyfill: false, + }, + ]); }); }); }); @@ -2569,7 +2719,7 @@ describe('DependencyGraph', function() { it('should work with nested node_modules', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -2577,15 +2727,15 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -2594,7 +2744,7 @@ describe('DependencyGraph', function() { }, }, }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -2609,53 +2759,55 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['foo', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: '/root/node_modules/foo/main.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: '/root/node_modules/foo/node_modules/bar/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: '/root/node_modules/bar/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: '/root/node_modules/foo/main.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: '/root/node_modules/foo/node_modules/bar/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: '/root/node_modules/bar/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('platform should work with node_modules', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': [ '/**', ' * @providesModule index', @@ -2663,14 +2815,14 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', }), 'index.ios.js': '', }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main', @@ -2685,44 +2837,46 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.ios.js', - dependencies: ['foo', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/index.ios.js', - path: '/root/node_modules/foo/index.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.ios.js', - path: '/root/node_modules/bar/main.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.ios.js', + dependencies: ['foo', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/index.ios.js', + path: '/root/node_modules/foo/index.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.ios.js', + path: '/root/node_modules/bar/main.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('nested node_modules with specific paths', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -2730,15 +2884,15 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar/");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar/lol");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -2748,7 +2902,7 @@ describe('DependencyGraph', function() { }, }, }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -2763,53 +2917,55 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['foo', 'bar/'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: '/root/node_modules/foo/main.js', - dependencies: ['bar/lol'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/lol.js', - path: '/root/node_modules/foo/node_modules/bar/lol.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: '/root/node_modules/bar/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo', 'bar/'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: '/root/node_modules/foo/main.js', + dependencies: ['bar/lol'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/lol.js', + path: '/root/node_modules/foo/node_modules/bar/lol.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: '/root/node_modules/bar/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('nested node_modules with browser field', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -2817,15 +2973,15 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar/lol");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -2839,7 +2995,7 @@ describe('DependencyGraph', function() { }, }, }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', browser: './main2', @@ -2854,61 +3010,63 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['foo', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: '/root/node_modules/foo/main.js', - dependencies: ['bar/lol'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/lol.js', - path: '/root/node_modules/foo/node_modules/bar/lol.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main2.js', - path: '/root/node_modules/bar/main2.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: '/root/node_modules/foo/main.js', + dependencies: ['bar/lol'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/lol.js', + path: '/root/node_modules/foo/node_modules/bar/lol.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main2.js', + path: '/root/node_modules/bar/main2.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('node_modules should support multi level', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', @@ -2916,8 +3074,8 @@ describe('DependencyGraph', function() { 'main.js': '', }, }, - 'path': { - 'to': { + path: { + to: { 'bar.js': [ '/**', ' * @providesModule bar', @@ -2925,7 +3083,7 @@ describe('DependencyGraph', function() { 'require("foo")', ].join('\n'), }, - 'node_modules': {}, + node_modules: {}, }, }, }); @@ -2934,37 +3092,39 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar', - path: '/root/path/to/bar.js', - dependencies: ['foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: '/root/node_modules/foo/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar', + path: '/root/path/to/bar.js', + dependencies: ['foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: '/root/node_modules/foo/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -2972,7 +3132,7 @@ describe('DependencyGraph', function() { var root = '/root'; var otherRoot = '/anotherRoot'; const filesystem = { - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -2984,7 +3144,7 @@ describe('DependencyGraph', function() { 'require("internalVendoredPackage");', 'require("anotherIndex");', ].join('\n'), - 'node_modules': { + node_modules: { 'react-haste': { 'package.json': JSON.stringify({ name: 'react-haste', @@ -2997,21 +3157,21 @@ describe('DependencyGraph', function() { ' */', 'require("submodule");', ].join('\n'), - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', }), // @providesModule should be ignored here, because it's not whitelisted - 'main.js':[ + 'main.js': [ '/**', ' * @providesModule dontWork', ' */', 'hi();', ].join('\n'), }, - 'submodule': { + submodule: { 'package.json': JSON.stringify({ name: 'submodule', main: 'main.js', @@ -3020,14 +3180,14 @@ describe('DependencyGraph', function() { }, }, }, - 'ember': { + ember: { 'package.json': JSON.stringify({ name: 'ember', main: 'main.js', }), // @providesModule should be ignored here, because it's not whitelisted, // and also, the modules "id" should be ember/main.js, not it's haste name - 'main.js':[ + 'main.js': [ '/**', ' * @providesModule wontWork', ' */', @@ -3037,14 +3197,14 @@ describe('DependencyGraph', function() { }, // This part of the dep graph is meant to emulate internal facebook infra. // By whitelisting `vendored_modules`, haste should still work. - 'vendored_modules': { + vendored_modules: { 'a-vendored-package': { 'package.json': JSON.stringify({ name: 'a-vendored-package', main: 'main.js', }), // @providesModule should _not_ be ignored here, because it's whitelisted. - 'main.js':[ + 'main.js': [ '/**', ' * @providesModule internalVendoredPackage', ' */', @@ -3054,7 +3214,7 @@ describe('DependencyGraph', function() { }, }, // we need to support multiple roots and using haste between them - 'anotherRoot': { + anotherRoot: { 'index.js': [ '/**', ' * @providesModule anotherIndex', @@ -3069,80 +3229,84 @@ describe('DependencyGraph', function() { ...defaults, roots: [root, otherRoot], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').catch( - error => { + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .catch(error => { expect(error.type).toEqual('UnableToResolveError'); - } - ).then(() => { - filesystem.root['index.js'] = filesystem.root['index.js'] - .replace('require("dontWork")', '') - .replace('require("wontWork")', ''); - return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js') - .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) - .then(deps => { - expect(deps).toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: [ - 'shouldWork', - 'ember', - 'internalVendoredPackage', - 'anotherIndex', - ], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'shouldWork', - path: '/root/node_modules/react-haste/main.js', - dependencies: ['submodule'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'submodule/main.js', - path: '/root/node_modules/react-haste/node_modules/submodule/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'ember/main.js', - path: '/root/node_modules/ember/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'internalVendoredPackage', - path: '/root/vendored_modules/a-vendored-package/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'anotherIndex', - path: '/anotherRoot/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); - }); - }); + }) + .then(() => { + filesystem.root['index.js'] = filesystem.root['index.js'] + .replace('require("dontWork")', '') + .replace('require("wontWork")', ''); + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/index.js', + ) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(deps => { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: [ + 'shouldWork', + 'ember', + 'internalVendoredPackage', + 'anotherIndex', + ], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'shouldWork', + path: '/root/node_modules/react-haste/main.js', + dependencies: ['submodule'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'submodule/main.js', + path: '/root/node_modules/react-haste/node_modules/submodule/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'ember/main.js', + path: '/root/node_modules/ember/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'internalVendoredPackage', + path: '/root/vendored_modules/a-vendored-package/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'anotherIndex', + path: '/anotherRoot/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); + }); + }); }); it('should not be confused by prev occuring whitelisted names', function() { @@ -3155,17 +3319,15 @@ describe('DependencyGraph', function() { ' */', 'require("shouldWork");', ].join('\n'), - 'node_modules': { + node_modules: { 'react-haste': { 'package.json': JSON.stringify({ name: 'react-haste', main: 'main.js', }), - 'main.js': [ - '/**', - ' * @providesModule shouldWork', - ' */', - ].join('\n'), + 'main.js': ['/**', ' * @providesModule shouldWork', ' */'].join( + '\n', + ), }, }, }, @@ -3175,42 +3337,44 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/react-haste/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/react-haste/index.js', - dependencies: ['shouldWork'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'shouldWork', - path: '/react-haste/node_modules/react-haste/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/react-haste/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/react-haste/index.js', + dependencies: ['shouldWork'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'shouldWork', + path: '/react-haste/node_modules/react-haste/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with node packages with a .js in the name', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("sha.js")', ].join('\n'), - 'node_modules': { + node_modules: { 'sha.js': { 'package.json': JSON.stringify({ name: 'sha.js', @@ -3226,35 +3390,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['sha.js'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'sha.js/main.js', - path: '/root/node_modules/sha.js/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['sha.js'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'sha.js/main.js', + path: '/root/node_modules/sha.js/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with multiple platforms (haste)', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': ` /** * @providesModule index @@ -3283,35 +3449,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.ios.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: '/root/a.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.ios.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: '/root/a.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should pick the generic file', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': ` /** * @providesModule index @@ -3341,35 +3509,37 @@ describe('DependencyGraph', function() { platforms: new Set(['ios', 'android', 'web']), roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.ios.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: '/root/a.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.ios.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: '/root/a.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with multiple platforms (node)', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': ` /** * @providesModule index @@ -3386,35 +3556,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.ios.js', - dependencies: ['./a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: '/root/a.ios.js', - path: '/root/a.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.ios.js', + dependencies: ['./a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: '/root/a.ios.js', + path: '/root/a.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should require package.json', () => { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -3422,14 +3594,14 @@ describe('DependencyGraph', function() { 'require("foo/package.json");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -3444,47 +3616,48 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['foo/package.json', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/package.json', - path: '/root/node_modules/foo/package.json', - dependencies: [], - isAsset: false, - isJSON: true, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: '/root/node_modules/bar/main.js', - dependencies: ['./package.json'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/package.json', - path: '/root/node_modules/bar/package.json', - dependencies: [], - isAsset: false, - isJSON: true, - isPolyfill: false, - resolution: undefined, - }, - - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo/package.json', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/package.json', + path: '/root/node_modules/foo/package.json', + dependencies: [], + isAsset: false, + isJSON: true, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: '/root/node_modules/bar/main.js', + dependencies: ['./package.json'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/package.json', + path: '/root/node_modules/bar/package.json', + dependencies: [], + isAsset: false, + isJSON: true, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -3493,8 +3666,8 @@ describe('DependencyGraph', function() { setMockFileSystem({ [root.slice(1)]: { 'index.js': 'require("a/index.js");', - 'node_modules': { - 'a': { + node_modules: { + a: { 'package.json': '{"name": "a", "version": "1.2.3"}', 'index.js': '', }, @@ -3506,28 +3679,30 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: '/root/index.js', - path: '/root/index.js', - dependencies: ['a/index.js'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a/index.js', - path: '/root/node_modules/a/index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: '/root/index.js', + path: '/root/index.js', + dependencies: ['a/index.js'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a/index.js', + path: '/root/node_modules/a/index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); }); @@ -3537,14 +3712,16 @@ describe('DependencyGraph', function() { // these tests will not work in a simulated way on linux testing VMs // due to the drive letter expectation - if (realPlatform !== 'win32') { return; } + if (realPlatform !== 'win32') { + return; + } const DependencyGraph = require('../DependencyGraph'); it('should work with nested node_modules', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -3552,15 +3729,15 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -3569,7 +3746,7 @@ describe('DependencyGraph', function() { }, }, }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -3584,53 +3761,55 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['foo', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: 'C:\\root\\node_modules\\foo\\main.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: 'C:\\root\\node_modules\\foo\\node_modules\\bar\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: 'C:\\root\\node_modules\\bar\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['foo', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: 'C:\\root\\node_modules\\foo\\main.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: 'C:\\root\\node_modules\\foo\\node_modules\\bar\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: 'C:\\root\\node_modules\\bar\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('platform should work with node_modules', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': [ '/**', ' * @providesModule index', @@ -3638,14 +3817,14 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', }), 'index.ios.js': '', }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main', @@ -3660,44 +3839,46 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.ios.js', - dependencies: ['foo', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/index.ios.js', - path: 'C:\\root\\node_modules\\foo\\index.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.ios.js', - path: 'C:\\root\\node_modules\\bar\\main.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.ios.js', + dependencies: ['foo', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/index.ios.js', + path: 'C:\\root\\node_modules\\foo\\index.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.ios.js', + path: 'C:\\root\\node_modules\\bar\\main.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('nested node_modules with specific paths', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -3705,15 +3886,15 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar/");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar/lol");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -3723,7 +3904,7 @@ describe('DependencyGraph', function() { }, }, }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -3738,53 +3919,55 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['foo', 'bar/'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: 'C:\\root\\node_modules\\foo\\main.js', - dependencies: ['bar/lol'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/lol.js', - path: 'C:\\root\\node_modules\\foo\\node_modules\\bar\\lol.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: 'C:\\root\\node_modules\\bar\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['foo', 'bar/'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: 'C:\\root\\node_modules\\foo\\main.js', + dependencies: ['bar/lol'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/lol.js', + path: 'C:\\root\\node_modules\\foo\\node_modules\\bar\\lol.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: 'C:\\root\\node_modules\\bar\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('nested node_modules with browser field', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -3792,15 +3975,15 @@ describe('DependencyGraph', function() { 'require("foo");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar/lol");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -3814,7 +3997,7 @@ describe('DependencyGraph', function() { }, }, }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', browser: './main2', @@ -3829,61 +4012,63 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['foo', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: 'C:\\root\\node_modules\\foo\\main.js', - dependencies: ['bar/lol'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/lol.js', - path: 'C:\\root\\node_modules\\foo\\node_modules\\bar\\lol.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main2.js', - path: 'C:\\root\\node_modules\\bar\\main2.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['foo', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: 'C:\\root\\node_modules\\foo\\main.js', + dependencies: ['bar/lol'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/lol.js', + path: 'C:\\root\\node_modules\\foo\\node_modules\\bar\\lol.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main2.js', + path: 'C:\\root\\node_modules\\bar\\main2.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('node_modules should support multi level', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', @@ -3891,8 +4076,8 @@ describe('DependencyGraph', function() { 'main.js': '', }, }, - 'path': { - 'to': { + path: { + to: { 'bar.js': [ '/**', ' * @providesModule bar', @@ -3900,7 +4085,7 @@ describe('DependencyGraph', function() { 'require("foo")', ].join('\n'), }, - 'node_modules': {}, + node_modules: {}, }, }, }); @@ -3909,37 +4094,39 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar', - path: 'C:\\root\\path\\to\\bar.js', - dependencies: ['foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/main.js', - path: 'C:\\root\\node_modules\\foo\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar', + path: 'C:\\root\\path\\to\\bar.js', + dependencies: ['foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/main.js', + path: 'C:\\root\\node_modules\\foo\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -3947,7 +4134,7 @@ describe('DependencyGraph', function() { var root = '/root'; var otherRoot = '/anotherRoot'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -3959,7 +4146,7 @@ describe('DependencyGraph', function() { 'require("internalVendoredPackage");', 'require("anotherIndex");', ].join('\n'), - 'node_modules': { + node_modules: { 'react-haste': { 'package.json': JSON.stringify({ name: 'react-haste', @@ -3972,21 +4159,21 @@ describe('DependencyGraph', function() { ' */', 'require("submodule");', ].join('\n'), - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', }), // @providesModule should be ignored here, because it's not whitelisted - 'main.js':[ + 'main.js': [ '/**', ' * @providesModule dontWork', ' */', 'hi();', ].join('\n'), }, - 'submodule': { + submodule: { 'package.json': JSON.stringify({ name: 'submodule', main: 'main.js', @@ -3995,14 +4182,14 @@ describe('DependencyGraph', function() { }, }, }, - 'ember': { + ember: { 'package.json': JSON.stringify({ name: 'ember', main: 'main.js', }), // @providesModule should be ignored here, because it's not whitelisted, // and also, the modules "id" should be ember/main.js, not it's haste name - 'main.js':[ + 'main.js': [ '/**', ' * @providesModule wontWork', ' */', @@ -4012,14 +4199,14 @@ describe('DependencyGraph', function() { }, // This part of the dep graph is meant to emulate internal facebook infra. // By whitelisting `vendored_modules`, haste should still work. - 'vendored_modules': { + vendored_modules: { 'a-vendored-package': { 'package.json': JSON.stringify({ name: 'a-vendored-package', main: 'main.js', }), // @providesModule should _not_ be ignored here, because it's whitelisted. - 'main.js':[ + 'main.js': [ '/**', ' * @providesModule internalVendoredPackage', ' */', @@ -4029,7 +4216,7 @@ describe('DependencyGraph', function() { }, }, // we need to support multiple roots and using haste between them - 'anotherRoot': { + anotherRoot: { 'index.js': [ '/**', ' * @providesModule anotherIndex', @@ -4043,71 +4230,73 @@ describe('DependencyGraph', function() { ...defaults, roots: [root, otherRoot], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: [ - 'shouldWork', - 'dontWork', - 'wontWork', - 'ember', - 'internalVendoredPackage', - 'anotherIndex', - ], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'shouldWork', - path: 'C:\\root\\node_modules\\react-haste\\main.js', - dependencies: ['submodule'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'submodule/main.js', - path: 'C:\\root\\node_modules\\react-haste\\node_modules\\submodule\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'ember/main.js', - path: 'C:\\root\\node_modules\\ember\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'internalVendoredPackage', - path: 'C:\\root\\vendored_modules\\a-vendored-package\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'anotherIndex', - path: 'C:\\anotherRoot\\index.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: [ + 'shouldWork', + 'dontWork', + 'wontWork', + 'ember', + 'internalVendoredPackage', + 'anotherIndex', + ], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'shouldWork', + path: 'C:\\root\\node_modules\\react-haste\\main.js', + dependencies: ['submodule'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'submodule/main.js', + path: 'C:\\root\\node_modules\\react-haste\\node_modules\\submodule\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'ember/main.js', + path: 'C:\\root\\node_modules\\ember\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'internalVendoredPackage', + path: 'C:\\root\\vendored_modules\\a-vendored-package\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'anotherIndex', + path: 'C:\\anotherRoot\\index.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -4121,17 +4310,15 @@ describe('DependencyGraph', function() { ' */', 'require("shouldWork");', ].join('\n'), - 'node_modules': { + node_modules: { 'react-haste': { 'package.json': JSON.stringify({ name: 'react-haste', main: 'main.js', }), - 'main.js': [ - '/**', - ' * @providesModule shouldWork', - ' */', - ].join('\n'), + 'main.js': ['/**', ' * @providesModule shouldWork', ' */'].join( + '\n', + ), }, }, }, @@ -4141,28 +4328,30 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/react-haste/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\react-haste\\index.js', - dependencies: ['shouldWork'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'shouldWork', - path: 'C:\\react-haste\\node_modules\\react-haste\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/react-haste/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\react-haste\\index.js', + dependencies: ['shouldWork'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'shouldWork', + path: 'C:\\react-haste\\node_modules\\react-haste\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); @@ -4170,15 +4359,15 @@ describe('DependencyGraph', function() { // For example SourceMap.js implements it's own require system. var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("foo/lol");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', @@ -4193,33 +4382,35 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['foo/lol'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['foo/lol'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with node packages with a .js in the name', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("sha.js")', ].join('\n'), - 'node_modules': { + node_modules: { 'sha.js': { 'package.json': JSON.stringify({ name: 'sha.js', @@ -4235,35 +4426,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['sha.js'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'sha.js/main.js', - path: 'C:\\root\\node_modules\\sha.js\\main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['sha.js'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'sha.js/main.js', + path: 'C:\\root\\node_modules\\sha.js\\main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with multiple platforms (haste)', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': ` /** * @providesModule index @@ -4292,35 +4485,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.ios.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: 'C:\\root\\a.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.ios.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: 'C:\\root\\a.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should pick the generic file', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': ` /** * @providesModule index @@ -4349,35 +4544,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.ios.js', - dependencies: ['a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'a', - path: 'C:\\root\\a.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.ios.js', + dependencies: ['a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'a', + path: 'C:\\root\\a.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should work with multiple platforms (node)', function() { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.ios.js': ` /** * @providesModule index @@ -4394,35 +4591,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.ios.js').then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.ios.js', - dependencies: ['./a'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'C:\\root\\a.ios.js', - path: 'C:\\root\\a.ios.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.ios.js', + ).then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.ios.js', + dependencies: ['./a'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'C:\\root\\a.ios.js', + path: 'C:\\root\\a.ios.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); it('should require package.json', () => { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -4430,14 +4629,14 @@ describe('DependencyGraph', function() { 'require("foo/package.json");', 'require("bar");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), }, - 'bar': { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -4452,47 +4651,48 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(deps => { - expect(deps) - .toEqual([ - { - id: 'index', - path: 'C:\\root\\index.js', - dependencies: ['foo/package.json', 'bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'foo/package.json', - path: 'C:\\root\\node_modules\\foo\\package.json', - dependencies: [], - isAsset: false, - isJSON: true, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/main.js', - path: 'C:\\root\\node_modules\\bar\\main.js', - dependencies: ['./package.json'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar/package.json', - path: 'C:\\root\\node_modules\\bar\\package.json', - dependencies: [], - isAsset: false, - isJSON: true, - isPolyfill: false, - resolution: undefined, - }, - - ]); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(deps => { + expect(deps).toEqual([ + { + id: 'index', + path: 'C:\\root\\index.js', + dependencies: ['foo/package.json', 'bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'foo/package.json', + path: 'C:\\root\\node_modules\\foo\\package.json', + dependencies: [], + isAsset: false, + isJSON: true, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/main.js', + path: 'C:\\root\\node_modules\\bar\\main.js', + dependencies: ['./package.json'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar/package.json', + path: 'C:\\root\\node_modules\\bar\\package.json', + dependencies: [], + isAsset: false, + isJSON: true, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); }); @@ -4513,7 +4713,7 @@ describe('DependencyGraph', function() { it('updates module dependencies', function() { var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -4527,7 +4727,7 @@ describe('DependencyGraph', function() { ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -4541,33 +4741,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { - filesystem.root['index.js'] = - filesystem.root['index.js'].replace('require("foo")', ''); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function() { + filesystem.root['index.js'] = filesystem.root['index.js'].replace( + 'require("foo")', + '', + ); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js') .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) .then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/main.js', - path: '/root/aPackage/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/main.js', + path: '/root/aPackage/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); }); @@ -4575,7 +4779,7 @@ describe('DependencyGraph', function() { it('updates module dependencies on file change', function() { var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -4589,7 +4793,7 @@ describe('DependencyGraph', function() { ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -4603,33 +4807,37 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { - filesystem.root['index.js'] = - filesystem.root['index.js'].replace('require("foo")', ''); + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function() { + filesystem.root['index.js'] = filesystem.root['index.js'].replace( + 'require("foo")', + '', + ); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js') .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) .then(function(deps) { - expect(deps) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/main.js', - path: '/root/aPackage/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - ]); + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/main.js', + path: '/root/aPackage/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); }); }); }); @@ -4638,7 +4846,7 @@ describe('DependencyGraph', function() { expect.assertions(1); var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -4652,7 +4860,7 @@ describe('DependencyGraph', function() { ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -4666,7 +4874,10 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { + return getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ).then(function() { delete filesystem.root['foo.js']; return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.js') .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) @@ -4678,7 +4889,7 @@ describe('DependencyGraph', function() { expect.assertions(1); var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -4692,7 +4903,7 @@ describe('DependencyGraph', function() { ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -4706,67 +4917,77 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { - filesystem.root['bar.js'] = [ - '/**', - ' * @providesModule bar', - ' */', - 'require("foo")', - ].join('\n'); - return triggerAndProcessWatchEvent(dgraph, 'change', root + '/bar.js'); - }).then(() => { - filesystem.root.aPackage['main.js'] = 'require("bar")'; - return triggerAndProcessWatchEvent(dgraph, 'change', root + '/aPackage/main.js'); - }).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(function(deps) { - expect(deps).toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage', 'foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'aPackage/main.js', - path: '/root/aPackage/main.js', - dependencies: ['bar'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - }, - { - id: 'bar', - path: '/root/bar.js', - dependencies: ['foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'foo', - path: '/root/foo.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); - }); + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .then(function() { + filesystem.root['bar.js'] = [ + '/**', + ' * @providesModule bar', + ' */', + 'require("foo")', + ].join('\n'); + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/bar.js', + ); + }) + .then(() => { + filesystem.root.aPackage['main.js'] = 'require("bar")'; + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/aPackage/main.js', + ); + }) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage', 'foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'aPackage/main.js', + path: '/root/aPackage/main.js', + dependencies: ['bar'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { + id: 'bar', + path: '/root/bar.js', + dependencies: ['foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'foo', + path: '/root/foo.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); + }); }); it('updates module dependencies on relative asset add', function() { var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -4785,54 +5006,57 @@ describe('DependencyGraph', function() { assetExts: ['png'], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').catch( - error => { + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .catch(error => { expect(error.type).toEqual('UnableToResolveError'); - } - ).then(() => { - filesystem.root['foo.png'] = ''; - return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.png'); - }).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(function(deps2) { - expect(deps2) - .toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['./foo.png'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'aPackage/foo.png', - path: '/root/foo.png', - dependencies: [], - isAsset: true, - resolution: 1, - isJSON: false, - isPolyfill: false, - resolveDependency: undefined, - }, - ]); - }); + }) + .then(() => { + filesystem.root['foo.png'] = ''; + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/foo.png', + ); + }) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(function(deps2) { + expect(deps2).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['./foo.png'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'aPackage/foo.png', + path: '/root/foo.png', + dependencies: [], + isAsset: true, + resolution: 1, + isJSON: false, + isPolyfill: false, + resolveDependency: undefined, + }, + ]); + }); }); it('changes to browser field', function() { expect.assertions(1); var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -4847,52 +5071,57 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { - filesystem.root.aPackage['package.json'] = JSON.stringify({ - name: 'aPackage', - main: 'main.js', - browser: 'browser.js', + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .then(function() { + filesystem.root.aPackage['package.json'] = JSON.stringify({ + name: 'aPackage', + main: 'main.js', + browser: 'browser.js', + }); + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/aPackage/package.json', + ); + }) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(function(deps) { + expect(deps).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'aPackage/browser.js', + path: '/root/aPackage/browser.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); }); - return triggerAndProcessWatchEvent(dgraph, 'change', root + '/aPackage/package.json'); - }).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(function(deps) { - expect(deps).toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['aPackage'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'aPackage/browser.js', - path: '/root/aPackage/browser.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); - }); }); it('removes old package from cache', function() { var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("aPackage")', ].join('\n'), - 'aPackage': { + aPackage: { 'package.json': JSON.stringify({ name: 'aPackage', main: 'main.js', @@ -4907,69 +5136,73 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { - filesystem.root['index.js'] = [ - '/**', - ' * @providesModule index', - ' */', - 'require("bPackage")', - ].join('\n'); - filesystem.root.aPackage['package.json'] = JSON.stringify({ - name: 'bPackage', - main: 'main.js', + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .then(function() { + filesystem.root['index.js'] = [ + '/**', + ' * @providesModule index', + ' */', + 'require("bPackage")', + ].join('\n'); + filesystem.root.aPackage['package.json'] = JSON.stringify({ + name: 'bPackage', + main: 'main.js', + }); + return dgraph.then( + dg => + new Promise(resolve => { + dg.once('change', () => resolve()); + triggerWatchEvent('change', root + '/index.js'); + triggerWatchEvent('change', root + '/aPackage/package.json'); + }), + ); + }) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(function(deps) { + expect(deps).toEqual([ + { + dependencies: ['bPackage'], + id: 'index', + isAsset: false, + isJSON: false, + isPolyfill: false, + path: '/root/index.js', + resolution: undefined, + resolveDependency: undefined, + }, + { + dependencies: [], + id: 'bPackage/main.js', + isAsset: false, + isJSON: false, + isPolyfill: false, + path: '/root/aPackage/main.js', + resolution: undefined, + }, + ]); }); - return dgraph.then(dg => new Promise(resolve => { - dg.once('change', () => resolve()); - triggerWatchEvent('change', root + '/index.js'); - triggerWatchEvent('change', root + '/aPackage/package.json'); - })); - }).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(function(deps) { - expect(deps).toEqual([ - { - dependencies: ['bPackage'], - id: 'index', - isAsset: false, - isJSON: false, - isPolyfill: false, - path: '/root/index.js', - resolution: undefined, - resolveDependency: undefined, - }, - { - dependencies: [], - id: 'bPackage/main.js', - isAsset: false, - isJSON: false, - isPolyfill: false, - path: '/root/aPackage/main.js', - resolution: undefined, - }, - ]); - }); }); it('should update node package changes', function() { expect.assertions(2); var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("foo");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', }), 'main.js': 'require("bar");\n/* foo module */', - 'node_modules': { - 'bar': { + node_modules: { + bar: { 'package.json': JSON.stringify({ name: 'bar', main: 'main.js', @@ -4986,9 +5219,9 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - expect(deps) - .toEqual([ + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .then(function(deps) { + expect(deps).toEqual([ { id: 'index', path: '/root/index.js', @@ -5021,49 +5254,53 @@ describe('DependencyGraph', function() { }, ]); - filesystem.root.node_modules.foo['main.js'] = 'lol'; - return triggerAndProcessWatchEvent(dgraph, 'change', root + '/node_modules/foo/main.js'); - }).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(function(deps2) { - expect(deps2).toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'foo/main.js', - path: '/root/node_modules/foo/main.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); - }); + filesystem.root.node_modules.foo['main.js'] = 'lol'; + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/node_modules/foo/main.js', + ); + }) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(function(deps2) { + expect(deps2).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'foo/main.js', + path: '/root/node_modules/foo/main.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); + }); }); it('should update node package main changes', function() { expect.assertions(1); var root = '/root'; var filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', 'require("foo");', ].join('\n'), - 'node_modules': { - 'foo': { + node_modules: { + foo: { 'package.json': JSON.stringify({ name: 'foo', main: 'main.js', @@ -5079,50 +5316,51 @@ describe('DependencyGraph', function() { ...defaults, roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { - filesystem.root.node_modules.foo['package.json'] = JSON.stringify({ - name: 'foo', - main: 'main.js', - browser: 'browser.js', + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .then(function(deps) { + filesystem.root.node_modules.foo['package.json'] = JSON.stringify({ + name: 'foo', + main: 'main.js', + browser: 'browser.js', + }); + return triggerAndProcessWatchEvent( + dgraph, + 'change', + root + '/node_modules/foo/package.json', + ); + }) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(function(deps2) { + expect(deps2).toEqual([ + { + id: 'index', + path: '/root/index.js', + dependencies: ['foo'], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + { + id: 'foo/browser.js', + path: '/root/node_modules/foo/browser.js', + dependencies: [], + isAsset: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + resolveDependency: undefined, + }, + ]); }); - return triggerAndProcessWatchEvent( - dgraph, - 'change', - root + '/node_modules/foo/package.json', - ); - }).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(function(deps2) { - expect(deps2).toEqual([ - { - id: 'index', - path: '/root/index.js', - dependencies: ['foo'], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - { - id: 'foo/browser.js', - path: '/root/node_modules/foo/browser.js', - dependencies: [], - isAsset: false, - isJSON: false, - isPolyfill: false, - resolution: undefined, - resolveDependency: undefined, - }, - ]); - }); }); it('should not error when the watcher reports a known file as added', function() { expect.assertions(1); var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', @@ -5143,47 +5381,38 @@ describe('DependencyGraph', function() { roots: [root], }); - return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then( - () => triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js'), - ).then( - () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'), - ).then(deps => { - expect(deps).toBeDefined(); - }); + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') + .then(() => + triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js'), + ) + .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js')) + .then(deps => { + expect(deps).toBeDefined(); + }); }); it('should recover from multiple modules with the same name (but this is broken right now)', async () => { const root = '/root'; console.warn = jest.fn(); const filesystem = setMockFileSystem({ - 'root': { + root: { 'index.js': [ '/**', ' * @providesModule index', ' */', - 'require(\'a\')', - 'require(\'b\')', - ].join('\n'), - 'a.js': [ - '/**', - ' * @providesModule a', - ' */', - ].join('\n'), - 'b.js': [ - '/**', - ' * @providesModule b', - ' */', + "require('a')", + "require('b')", ].join('\n'), + 'a.js': ['/**', ' * @providesModule a', ' */'].join('\n'), + 'b.js': ['/**', ' * @providesModule b', ' */'].join('\n'), }, }); const dgraph = DependencyGraph.load({...defaults, roots: [root]}); await getOrderedDependenciesAsJSON(dgraph, root + '/index.js'); - filesystem.root['b.js'] = [ - '/**', - ' * @providesModule a', - ' */', - ].join('\n'); + filesystem.root['b.js'] = ['/**', ' * @providesModule a', ' */'].join( + '\n', + ); await triggerAndProcessWatchEvent(dgraph, 'change', root + '/b.js'); try { await getOrderedDependenciesAsJSON(dgraph, root + '/index.js'); @@ -5193,11 +5422,9 @@ describe('DependencyGraph', function() { throw error; } expect(console.warn).toBeCalled(); - filesystem.root['b.js'] = [ - '/**', - ' * @providesModule b', - ' */', - ].join('\n'); + filesystem.root['b.js'] = ['/**', ' * @providesModule b', ' */'].join( + '\n', + ); await triggerAndProcessWatchEvent(dgraph, 'change', root + '/b.js'); } @@ -5213,7 +5440,6 @@ describe('DependencyGraph', function() { } } }); - }); describe('Extensions', () => { @@ -5231,18 +5457,14 @@ describe('DependencyGraph', function() { it('supports custom file extensions', () => { var root = '/root'; setMockFileSystem({ - 'root': { + root: { 'index.jsx': [ '/**', ' * @providesModule index', ' */', 'require("a")', ].join('\n'), - 'a.coffee': [ - '/**', - ' * @providesModule a', - ' */', - ].join('\n'), + 'a.coffee': ['/**', ' * @providesModule a', ' */'].join('\n'), 'X.js': '', }, }); @@ -5256,9 +5478,7 @@ describe('DependencyGraph', function() { return dgraph .then(dg => dg.matchFilesByPattern('.*')) .then(files => { - expect(files).toEqual([ - '/root/index.jsx', '/root/a.coffee', - ]); + expect(files).toEqual(['/root/index.jsx', '/root/a.coffee']); }) .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.jsx')) .then(deps => { @@ -5288,12 +5508,9 @@ describe('DependencyGraph', function() { it('supports custom file extensions with relative paths', async () => { const root = '/root'; setMockFileSystem({ - 'root': { - 'index.jsx': [ - 'require("./a")', - ].join('\n'), - 'a.coffee': [ - ].join('\n'), + root: { + 'index.jsx': ['require("./a")'].join('\n'), + 'a.coffee': [].join('\n'), 'X.js': '', }, }); @@ -5304,11 +5521,12 @@ describe('DependencyGraph', function() { sourceExts: ['jsx', 'coffee'], }); const files = await dgraph.matchFilesByPattern('.*'); - expect(files).toEqual([ - '/root/index.jsx', '/root/a.coffee', - ]); + expect(files).toEqual(['/root/index.jsx', '/root/a.coffee']); - const deps = await getOrderedDependenciesAsJSON(dgraph, '/root/index.jsx'); + const deps = await getOrderedDependenciesAsJSON( + dgraph, + '/root/index.jsx', + ); expect(deps).toEqual([ { dependencies: ['./a'], @@ -5334,12 +5552,9 @@ describe('DependencyGraph', function() { it('does not include extensions that are not specified explicitely', async () => { const root = '/root'; setMockFileSystem({ - 'root': { - 'index.jsx': [ - 'require("./a")', - ].join('\n'), - 'a.coffee': [ - ].join('\n'), + root: { + 'index.jsx': ['require("./a")'].join('\n'), + 'a.coffee': [].join('\n'), 'X.js': '', }, }); @@ -5364,11 +5579,13 @@ describe('DependencyGraph', function() { let dependencyGraph, onProgress; function makeModule(id, dependencies = []) { - return ` + return ( + ` /** * @providesModule ${id} */\n` + - dependencies.map(d => `require(${JSON.stringify(d)});`).join('\n'); + dependencies.map(d => `require(${JSON.stringify(d)});`).join('\n') + ); } function getDependencies() { @@ -5382,7 +5599,7 @@ describe('DependencyGraph', function() { beforeEach(function() { onProgress = jest.genMockFn(); setMockFileSystem({ - 'root': { + root: { 'index.js': makeModule('index', ['a', 'b']), 'a.js': makeModule('a', ['c', 'd']), 'b.js': makeModule('b', ['d', 'e']), @@ -5397,12 +5614,14 @@ describe('DependencyGraph', function() { return DependencyGraph.load({ ...defaults, roots: ['/root'], - }).then(dg => { dependencyGraph = dg; }); + }).then(dg => { + dependencyGraph = dg; + }); }); it('calls back for each finished module', () => { return getDependencies().then(() => - expect(onProgress.mock.calls.length).toBe(8) + expect(onProgress.mock.calls.length).toBe(8), ); }); @@ -5431,9 +5650,9 @@ describe('DependencyGraph', function() { const assetDependencies = ['/root/apple.png', '/root/banana.png']; setMockFileSystem({ - 'root': { + root: { 'index.js': 'require("./a.png")', - 'a.png' : '', + 'a.png': '', 'apple.png': '', 'banana.png': '', }, @@ -5443,13 +5662,18 @@ describe('DependencyGraph', function() { ...defaults, assetDependencies, roots: ['/root'], - }).then(dependencyGraph => dependencyGraph.getDependencies({ - entryPath: '/root/index.js', - })).then(({dependencies}) => { - const [, assetModule] = dependencies; - return assetModule.getDependencies() - .then(deps => expect(deps).toBe(assetDependencies)); - }); + }) + .then(dependencyGraph => + dependencyGraph.getDependencies({ + entryPath: '/root/index.js', + }), + ) + .then(({dependencies}) => { + const [, assetModule] = dependencies; + return assetModule + .getDependencies() + .then(deps => expect(deps).toBe(assetDependencies)); + }); }); }); @@ -5462,7 +5686,7 @@ describe('DependencyGraph', function() { moduleRead = Module.prototype.read; DependencyGraph = require('../DependencyGraph'); setMockFileSystem({ - 'root': { + root: { 'index.js': ` require('./a'); require('./b'); @@ -5509,7 +5733,10 @@ describe('DependencyGraph', function() { }); it('produces a deterministic tree if the "a" module resolves first', () => { - const dependenciesPromise = getOrderedDependenciesAsJSON(dependencyGraph, 'index.js'); + const dependenciesPromise = getOrderedDependenciesAsJSON( + dependencyGraph, + 'index.js', + ); return Promise.all(callDeferreds.map(deferred => deferred.promise)) .then(() => { @@ -5519,8 +5746,11 @@ describe('DependencyGraph', function() { }); main.resolve(); return dependenciesPromise; - }).then(result => { - const names = result.map(({path: resultPath}) => resultPath.split('/').pop()); + }) + .then(result => { + const names = result.map(({path: resultPath}) => + resultPath.split('/').pop(), + ); expect(names).toEqual([ 'index.js', 'a.js', @@ -5534,7 +5764,10 @@ describe('DependencyGraph', function() { }); it('produces a deterministic tree if the "b" module resolves first', () => { - const dependenciesPromise = getOrderedDependenciesAsJSON(dependencyGraph, 'index.js'); + const dependenciesPromise = getOrderedDependenciesAsJSON( + dependencyGraph, + 'index.js', + ); return Promise.all(callDeferreds.map(deferred => deferred.promise)) .then(() => { @@ -5544,8 +5777,11 @@ describe('DependencyGraph', function() { }); main.resolve(); return dependenciesPromise; - }).then(result => { - const names = result.map(({path: resultPath}) => resultPath.split('/').pop()); + }) + .then(result => { + const names = result.map(({path: resultPath}) => + resultPath.split('/').pop(), + ); expect(names).toEqual([ 'index.js', 'a.js', @@ -5561,7 +5797,9 @@ describe('DependencyGraph', function() { function defer(value) { let resolve; - const promise = new Promise(r => { resolve = r; }); + const promise = new Promise(r => { + resolve = r; + }); return {promise, resolve: () => resolve(value)}; } @@ -5570,10 +5808,13 @@ describe('DependencyGraph', function() { } function triggerAndProcessWatchEvent(dgraphPromise, eventType, filename) { - return dgraphPromise.then(dgraph => new Promise(resolve => { - dgraph.once('change', () => resolve()); - triggerWatchEvent(eventType, filename); - })); + return dgraphPromise.then( + dgraph => + new Promise(resolve => { + dgraph.once('change', () => resolve()); + triggerWatchEvent(eventType, filename); + }), + ); } function triggerWatchEvent(eventType, filename) {