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
This commit is contained in:
Jean Lauliac 2017-06-14 06:57:19 -07:00 committed by Facebook Github Bot
parent e87a8205d8
commit 4012f96c25
2 changed files with 25 additions and 9 deletions

View File

@ -220,6 +220,10 @@ class DependencyGraph extends EventEmitter {
return this._haste; return this._haste;
} }
end() {
this._haste.end();
}
/** /**
* Returns the module object for the given path. * Returns the module object for the given path.
*/ */

View File

@ -135,7 +135,21 @@ describe('DependencyGraph', function() {
process.platform = realPlatform; 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'; var root = '/root';
setMockFileSystem({ setMockFileSystem({
root: { root: {
@ -152,14 +166,12 @@ describe('DependencyGraph', function() {
}, },
}); });
var dgraph = DependencyGraph.load({ const opts = {...defaults, roots: [root]};
...defaults, return await processDgraph(opts, async dgraph => {
roots: [root], const deps = await getOrderedDependenciesAsJSON(
}); dgraph,
return getOrderedDependenciesAsJSON( '/root/index.js',
dgraph, );
'/root/index.js',
).then(function(deps) {
expect(deps).toEqual([ expect(deps).toEqual([
{ {
id: 'index', id: 'index',