New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Widget test with await is hanged when not using runAsync
instead of showing error/warning message
#5728
Comments
testWidgets is using a FakeAsync, and setUp's future is created outside that FakeAsync scope. |
This does look zone-related. I couldn't reproduce using just
Here's the plain code I used: import "dart:async";
import "package:quiver/testing/async.dart";
import "package:test/test.dart";
void main() {
Future future;
zoneChain() {
var zone = Zone.current;
var zones = [];
while(zone != null) {
zones.add(zone);
zone = zone.parent;
}
return zones.map((z) => z.hashCode).join(' < ');
}
setUp(() {
print('setUp zones: ${zoneChain()}');
future = new Future.value();
});
test("awaiting future with value from setUp works", () async {
var fakeAsync = new FakeAsync();
fakeAsync.run((_) async {
print('test zones: ${zoneChain()}');
await future;
});
fakeAsync.flushMicrotasks();
});
} |
@nex3 any ideas? |
Do we think this is a package:test bug, should we move it there? |
we've no idea. |
Is this still an issue? |
From what I understand you are supposed to use This works: testWidgets("awaiting future with value from setUp works",
(WidgetTester tester) async {
await tester.runAsync(() async {
await future;
});
}); Edit: added |
I believe @mdebbar saw this recently too. Yes, any real asynchronous work has to go through |
And don’t forget to await on runAsync: await tester.runAsync(() async {
await future;
}); |
Based on #5728 (comment), test still get hanged when not Code Sample
flutter doctor -v
|
runAsync
instead of showing error/warning message
So it is on purpose that I came here because I wanted to extract creating a bloc (and closing it) inside a test into a Old code: testWidgets('placeholder is shown on open homework page when no homeworks are to do',
(tester) async {
// Creating it here
final homeworkPageBloc = MockTeacherHomeworkPageBloc();
await _pumpHomeworkPageWithPlaceholder(tester,
bloc: homeworkPageBloc, initialTab: HomeworkTab.open);
expect(
find.byKey(
ValueKey('no-homework-teacher-placeholder-for-open-homework')),
findsOneWidget);
homeworkPageBloc.close();
}); New code: setUp(() {
// Creating it here
homeworkPageBloc = MockTeacherHomeworkPageBloc();
});
testWidgets('placeholder is shown on open homework page when no homeworks are to do',
(tester) async {
await pumpHomeworkPage(tester,
bloc: homeworkPageBloc, initialTab: HomeworkTab.open);
homeworkPageBloc.emitNewState(_noHomeworks);
await tester.pump();
expect(
find.byKey(
ValueKey('no-homework-teacher-placeholder-for-open-homework')),
findsOneWidget);
homeworkPageBloc.close();
}); But when I do this the test will fail. I already wasted so much time because this behavior is non-obvious. Especially if you have complexity through something like a bloc. As a normal flutter user you just don't know all this behavior. I still don't understand why this test fails exactly.
class MockTeacherHomeworkPageBloc extends HomeworkPageBloc {
final _queuedStates = Queue<HomeworkPageState>();
void emitNewState(HomeworkPageState state) {
_queuedStates.add(state);
add(LoadHomeworks());
}
@override
Stream<HomeworkPageState> mapEventToState(HomeworkPageEvent event) async* {
if (_queuedStates.isNotEmpty) {
yield _queuedStates.removeFirst();
}
}
@override
HomeworkPageState initialState = Loading();
} In |
@Jonas-Sander I think this is maybe catching out new Flutter developers because the way that widget tests run inside zones created by FakeAsync and then how FakeAsync works with streams is not covered at all in the widget testing intro documentation. BUT I think you should raise that as a new issue as that's not really what this issue is about. |
And even this assumes that everyone reads the docs.
What would the premise of the new issue be exactly? The docs? The behavior overall? Do you have a recommendation for my case? Just skipping the |
- Change `testWidgets` to `test` to improve async handling (`testWidgets` uses FakeAsync: flutter/flutter#5728) - Fix newly-failing tests for `next` commit-id:0d79ab75
- Change `testWidgets` to `test` to improve async handling (`testWidgets` uses FakeAsync: flutter/flutter#5728) - Fix newly-failing tests for `next` commit-id:0d79ab75
- Change `testWidgets` to `test` to improve async handling (`testWidgets` uses FakeAsync: flutter/flutter#5728) - Fix newly-failing tests for `next` commit-id:0d79ab75
- Change `testWidgets` to `test` to improve async handling (`testWidgets` uses FakeAsync: flutter/flutter#5728) - Fix newly-failing tests for `next` commit-id:0d79ab75
- Change `testWidgets` to `test` to improve async handling (`testWidgets` uses FakeAsync: flutter/flutter#5728) - Fix newly-failing tests for `next` commit-id:0d79ab75
The text was updated successfully, but these errors were encountered: