Skip to content
pubscout

fake_async

1.3.3, released 2 years ago by dart.dev

Fake asynchronous events like timers and microtasks for deterministic testing.

WASM-readyQuiet but busy
$ flutter pub add fake_async
Source repository

What it does

Provides a FakeAsync class that makes it easy to deterministically test code using asynchronous features like Futures, Streams, Timers, and microtasks. It lets you explicitly control the notion of current time and fires scheduled events immediately without real time elapsing.

AI-written summary of version 1.3.3, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You want to test code containing time-based asynchronous events deterministically.
  • You need to avoid waiting for real time to pass during unit tests involving timeouts or delays.

Skip it when

  • You are writing integration tests requiring real hardware or network I/O that cannot be mocked.

Strengths

  • Allows explicit control over the current time in tests.
  • Integrates seamlessly with the clock package for controlling DateTime.now() and Stopwatch.

Weaknesses

  • Cannot control real hardware time or OS-level delays outside of Dart's asynchronous zone mechanisms.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2
fakeAsync((async) {
  expect(Completer().future.timeout(Duration(seconds: 5)),
      throwsA(isA<TimeoutException>()));
  async.elapse(Duration(seconds: 5));
});

Works with

Packages commonly used together with fake_async: ones it builds on, ones built on it, and its own family.