diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 517c9e11a4..64ff421065 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -125,9 +125,16 @@ void testUsingContext( Analytics: () => NoOpAnalytics(), }, body: () { - return runZonedGuarded>(() { + // To catch all errors thrown by the test, even uncaught async errors, we use a zone. + // + // Zones introduce their own event loop, so we do not await futures created inside + // the zone from outside the zone. Instead, we create a Completer outside the zone, + // and have the test complete it when the test ends (in success or failure), and we + // await that. + final Completer completer = Completer(); + runZonedGuarded>(() async { try { - return context.run( + return await context.run( // Apply the overrides to the test context in the zone since their // instantiation may reference items already stored on the context. overrides: overrides, @@ -145,14 +152,22 @@ void testUsingContext( } catch (error) { // ignore: avoid_catches_without_on_clauses _printBufferedErrors(context); rethrow; + } finally { + if (!completer.isCompleted) { + completer.complete(); + } } }, (Object error, StackTrace stackTrace) { // When things fail, it's ok to print to the console! print(error); // ignore: avoid_print print(stackTrace); // ignore: avoid_print _printBufferedErrors(context); + if (!completer.isCompleted) { + completer.completeError(error, stackTrace); + } throw error; //ignore: only_throw_errors }); + return completer.future; }, ); }, overrides: {