Flutter Hooks Testing Library

fishing pole

Simple and complete Flutter hooks testing utilities that encourage good testing practices.

Inspired by react-hooks-testing-library.


The first thing

This package has great respect for react-hooks-testing-library. So the idea is based on it.

The problem

You’re writing an awesome custom hook and you want to test it, but as soon as you call it you see
the following error:

Invariant Violation: Hooks can only be called inside the body of a function component.

You don’t really want to write a component solely for testing this hook and have to work out how you
were going to trigger all the various ways the hook can be updated, especially given the
complexities of how you’ve wired the whole thing together.

The solution

The Flutter Hooks Testing Library allows you to create a simple test harness for Flutter hooks that
handles running them within the body of a function component, as well as providing various useful
utility functions for updating the inputs and retrieving the outputs of your amazing custom hook.
This library aims to provide a testing experience as close as possible to natively using your hook
from within a real component.

Using this library, you do not have to concern yourself with how to construct, render or interact
with the flutter component in order to test your hook. You can just use the hook directly and assert
the results.

When to use this library

  1. You’re writing a library with one or more custom hooks that are not directly tied to a component
  2. You have a complex hook that is difficult to test through component interactions

When not to use this library

  1. Your hook is defined alongside a component and is only used there
  2. Your hook is easy to test by just testing the components using it


  flutter_hooks_test: ^0.0.1



VoidCallback useUpdate() {
  final attempt = useState(0);
  return () => attempt.value++;


Not using

testWidgets('should re-build component each time returned function is called', (tester) async {
  // Before
  const key = Key('button');
  var buildCount = 0;

  // called count is 1
  await tester.pumpWidget(HookBuilder(builder: (context) {
    final update = useUpdate();
    return GestureDetector(
      key: key,
      onTap: () => update(),
  // called count is 2
  await tester.tap(find.byKey(key));
  await tester.pumpAndSettle(const Duration(milliseconds: 1));
  expect(buildCount, 2);


testWidgets('should re-build component each time returned function is called', (tester) async {
  // After
  var buildCount = 0;
  final result = await buildHook((_) {
    return useUpdate();

  expect(buildCount, 1);
  final update = result.current;
  await act(() => update());
  expect(buildCount, 2);


Please file Flutter Hooks Testing Library specific issues, bugs, or feature requests in our issue tracker.

Plugin issues that are not specific to Flutter Hooks Testing Library can be filed in the Flutter issue tracker.


If you wish to contribute a change to any of the existing plugins in this repo,
please review our contribution guide
and open a pull request.


View Github