Skip to content

prefer_expect_later

v0.4.0 Warning Fix Testing Rules

Flags expect() calls where the first argument is a Future. Passing a Future to expect() instead of expectLater() means the assertion completes before the asynchronous operation finishes, causing the test to silently pass regardless of the actual result.

Using expect() with a Future is almost always a bug. The test framework cannot await a synchronous expect() call, so the assertion is evaluated against the Future object itself rather than its resolved value. Switching to await expectLater() ensures the Future completes before the matcher runs.

See also: test package - expectLater

Future<void> bad() async {
expect(Future.value(1), completion);
final future = Future.value(42);
expect(future, completion);
expect(fetchData(), completion);
}
Future<void> good() async {
await expectLater(Future.value(1), completion);
final future = Future.value(42);
await expectLater(future, completion);
// expect with non-Future values is fine:
expect(42, completion);
expect('hello', completion);
}

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_expect_later: false