Skip to content

Testing

Tests need an empty cache and control over time. Give each test a fresh QueryClient with retries turned off, and run it on a fake clock.

Give each test a fresh client, and turn off retries so a failure shows at once:

testWidgets('shows todos', (tester) async {
final client = QueryClient(
defaultOptions: const DefaultOptions(
queries: QueryDefaults(retry: RetryPolicy.never()),
),
);
Fuery.client = client;
await tester.pumpWidget(const App());
await tester.pump(const Duration(milliseconds: 500));
expect(find.text('Buy milk'), findsOneWidget);
await tester.pumpWidget(const SizedBox());
client.clear();
});
  • Pump the time your fake API takes. A fake that answers at once needs only await tester.pump().
  • End each test by unmounting the widgets and calling client.clear(). Otherwise the cache’s garbage collection timers are still pending, and the test fails.
  • Only queries need retries turned off. Mutations don’t retry unless you set retry, so QueryDefaults is enough. Add mutations: MutationDefaults(...) only when a test needs a mutation default of its own.
  • FueryProvider works too. Widgets use the client of the nearest FueryProvider, so FueryProvider(client: client, child: const App()) can replace assigning Fuery.client. observe() and a definition’s mutate and mutateAsync still use Fuery.client unless you pass a client. An observer that the test or a cubit creates then needs observe(client: client), and a widget that runs a mutation from its definition passes context.queryClient.
  • Create observers inside the test. An observer keeps its client, so one created at the top level of a file keeps the first test’s client.

Add fake_async as a dev dependency to control time in plain Dart tests:

test('loads todos', () {
fakeAsync((async) {
final client = QueryClient(
defaultOptions: const DefaultOptions(
queries: QueryDefaults(retry: RetryPolicy.never()),
),
);
final todos = Query(
queryKey: ['todos'],
queryFn: (_) async => ['Buy milk'],
).observe(client: client);
final subscription = todos.stream.listen((_) {});
async.flushMicrotasks();
expect(todos.result.data, ['Buy milk']);
subscription.cancel();
client.clear();
});
});

Listening to stream subscribes the observer and starts the fetch. For a fake that waits, advance the clock with async.elapse(...) instead of flushMicrotasks().

Test a cubit or a bloc that uses queries in testWidgets or fakeAsync, like the tests above.

  • Call subscription.cancel() in close() without await. Under a fake clock, the future that cancel() returns never completes, and the test hangs.
  • Close the cubit before client.clear(). clear() moves an observer that is still subscribed to a new query, which loads again.

focusManager.setFocused(false) makes Fuery treat the app as in the background: retries wait, and polling pauses unless refetchIntervalInBackground is set. focusManager.setFocused(null) hands focus back to the app lifecycle, and Fuery refetches the stale queries in use.

Every test in a file shares focusManager, so reset it in a tear-down. A tear-down runs after the widgets are gone, and it runs even when the test fails:

addTearDown(() => focusManager.setFocused(null));

The example’s tests open a tab or a post with a small helper, and its test folder has a widget test per screen.