From 4012f96c252d0e2f8cd3994a57b90de4f5aa5bea Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 14 Jun 2017 06:57:19 -0700 Subject: [PATCH] metro-bundler: DependencyGraph-test: proof of concept for proper test cleanup Summary: The `DependencyGraph-test` hangs forever if run in isolation because the watch mode is not properly ended. I propose we just wrap each test in a function that does the correct thing, and also migrate tests to `async`/`await`. Reviewed By: cpojer Differential Revision: D5245477 fbshipit-source-id: ea30c0e637e0c7b85afe4c76c5e985846ae9b243 --- .../src/node-haste/DependencyGraph.js | 4 +++ .../__tests__/DependencyGraph-test.js | 30 +++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/metro-bundler/src/node-haste/DependencyGraph.js b/packages/metro-bundler/src/node-haste/DependencyGraph.js index 8437fcaa..ac0c9540 100644 --- a/packages/metro-bundler/src/node-haste/DependencyGraph.js +++ b/packages/metro-bundler/src/node-haste/DependencyGraph.js @@ -220,6 +220,10 @@ class DependencyGraph extends EventEmitter { return this._haste; } + end() { + this._haste.end(); + } + /** * Returns the module object for the given path. */ 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 32c2eb98..b112bb2f 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js @@ -135,7 +135,21 @@ describe('DependencyGraph', function() { process.platform = realPlatform; }); - it('should get dependencies', function() { + /** + * When running a test on the dependency graph, watch mode is enabled by + * default, so we must end the watcher to ensure the test does not hang up + * (regardless if the test passes or fails). + */ + async function processDgraph(options, processor) { + const dgraph = await DependencyGraph.load(options); + try { + await processor(dgraph); + } finally { + dgraph.end(); + } + } + + it('should get dependencies', async function() { var root = '/root'; setMockFileSystem({ root: { @@ -152,14 +166,12 @@ describe('DependencyGraph', function() { }, }); - var dgraph = DependencyGraph.load({ - ...defaults, - roots: [root], - }); - return getOrderedDependenciesAsJSON( - dgraph, - '/root/index.js', - ).then(function(deps) { + const opts = {...defaults, roots: [root]}; + return await processDgraph(opts, async dgraph => { + const deps = await getOrderedDependenciesAsJSON( + dgraph, + '/root/index.js', + ); expect(deps).toEqual([ { id: 'index',