Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import 'package:flutter_test/flutter_test.dart';
void main() {
Future<int> outerF;
final Duration delay = Duration(seconds: 20);
group('Async Tests - Fake Async', () {
setUpAll(() async {
outerF = Future<int>.delayed(delay, () => 42);
final Stopwatch waiting = Stopwatch()..start();
final Future<void> innerF = Future<int>.delayed(Duration(seconds: 3), () {});
await innerF;
print('Waited for ${waiting.elapsed}');
// Each testWidgets callback is run in a distinct FakeAsync zone
testWidgets('Delayed in test, no time added', (WidgetTester tester) async {
// This will fail due to blowing past the 2 second test case timeout.
final Future<int> f = Future<int>.delayed(delay, () => 1);
await f;
testWidgets('Delayed in test, time added', (WidgetTester tester) async {
final Future<int> f = Future<int>.delayed(delay, () => 1);
// So we extend the "time" allowed in the zone, and now it passes.
// Note that this still runs almost immediately.
await f;
testWidgets('Delayed from setUp, no time added', (WidgetTester tester) async {
// This will fail, and will appear to be from a timeout.
await outerF;
testWidgets('Delayed from setUp, time added', (WidgetTester tester) async {
// Even with the "time" added though, it will fail. It will not resolve the
// future from a different, Real Async, Zone. The error will still look like
// an exception related to a timeout.
// It will also run very slow, at least as long as the amount of "time" the
// tester binding thinks it will take.
await outerF;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.