-
Notifications
You must be signed in to change notification settings - Fork 214
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
Prevent accidental misuse of fake_async when using async/await #2307
Labels
Comments
An alternative solution could be #2309 I think |
A duplicate report #2312 has some handy repro cases. This test correctly fails: test('test smoke test -- this test should fail', () {
fakeAsync((async) {
expect(true, isFalse);
});
}); But this one unexpectedly passes: test('test smoke test -- this test should fail', () async {
fakeAsync((async) async {
expect(true, isFalse);
});
}); Then here's a slightly more complex example, which one might expect to pass: import 'package:fake_async/fake_async.dart';
import 'package:test/test.dart';
void main() {
test('test smoke test -- this test should pass', () async {
await fakeAsync((async) async {
final future = doWork();
async.elapse(const Duration(seconds: 2));
final value = await future;
expect(value, 1);
});
});
}
Future<int> doWork() async {
await Future<void>.delayed(const Duration(seconds: 1));
return 1;
} but instead it gets stuck and times out:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use fakeAsync for some test cases in my daily work.
I have accidentally written a test case which looks like this:
I only realized a few weeks later (after the code went through a review by multiple experienced engineers and it was submitted), that the expectation doesn't actually run. I could add a
throw Exception()
after theexpect
call, and my test case would still succeed. This is because asynchronous callbacks passed to FakeAsync.run are effectively not awaited.Unfortunately I was not the only one making this mistake, but I have seen this in multiple code files.
I'm wondering whether this kind of errors could be prevented.
Here is a naive idea:
Please feel free to contact me if you need an example for the erroneous use-case or the possible solution.
The text was updated successfully, but these errors were encountered: