This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Flutter Hooks Test is a testing utility library for Flutter hooks, inspired by React's react-hooks-testing-library. It provides a simple API to test custom hooks in isolation.
# Install dependencies
melos get
bun install
# Run tests
melos test
# Run code analysis
melos analyze
# Format code (Dart + Prettier)
melos format
# Run all checks (analyze + format + test)
melos analyze && melos format && melos test
# Run a single test file
flutter test test/flutter_hooks_test_test.dart
# Run tests with coverage
flutter test --coverage# Upgrade dependencies
melos upgrade
# Clean build artifacts
melos clean
# Format with Prettier only
bun run format
# Setup git hooks
bun run prepareThe library exports a single file lib/flutter_hooks_test.dart containing:
-
buildHook<T>()- Test hooks without props- Generic
T: Return type of the hook - Returns
HookResult<T>with methods:current: Access current hook valuerebuild(): Trigger rebuild and update historyunmount(): Unmount the hookall: Build history for debuggingbuildCount: Number of builds
- Generic
-
buildHookWithProps<T, P>()- Test hooks with props- Generic
T: Return type,P: Props type - Returns
HookResultWithProps<T, P>with additional:rebuildWithProps(P props): Rebuild with new props
- Generic
-
act- Wraps state changes to ensure proper Flutter test lifecycle -
waitForutilities - Async testing helperswaitFor(condition): Wait for condition to be truewaitForValueToChange(getValue): Wait for value changeresult.waitForNextUpdate(): Wait for hook rebuildresult.waitForValueToMatch(predicate): Wait for specific condition- Similar to React's
actfunction - Required when changing hook state
// Basic hook test (no props)
final result = await buildHook(() => useMyHook());
await act(() => result.current.doSomething());
expect(result.current.value, expectedValue);
// Hook with props
final result = await buildHookWithProps(
(count) => useCounter(count),
initialProps: 5,
);
await result.rebuildWithProps(10);
expect(result.current.value, 10);
// With wrapper widget
final result = await buildHook(
() => useMyHook(),
wrapper: (child) => Provider(child: child),
);
// Async testing with waitFor
await act(() => result.current.startAsync());
await waitFor(() => !result.current.isLoading);
expect(result.current.data, isNotNull);
// History tracking
expect(result.buildCount, 1);
expect(result.all.length, 1);
await result.rebuild();
expect(result.buildCount, 2);- Uses
TestWidgetsFlutterBindingfor test environment - Creates minimal widget tree with
HookBuilder _BaseHookResult<T>base class eliminates code duplication- Build history tracking with automatic value capture
- Separate result classes for hooks with/without props
- Helper functions (
_applyWrapper) and constants (_kEmptyWidget)
- All tests go in
test/directory - Example hooks in
test/hooks/demonstrate testable patterns - Use
testWidgetsfor widget-based tests - Use Mockito for mocking dependencies
- Flutter lints are enforced via
analysis_options.yaml - Example directory is excluded from analysis
- Pre-commit hooks format code automatically
- CI runs on Ubuntu with asdf version management
- API Selection: Use
buildHook()for hooks without props,buildHookWithProps()for hooks with props - Type Safety: Generic types are automatically inferred in most cases
- Act Wrapper: Always wrap state changes in
act() - Async Testing: Use
waitForutilities for async operations - History Tracking: Use
result.allandresult.buildCountfor debugging - Rebuilds: Call
rebuild()after state changes to update history - Testing: Test mount/unmount/rebuild scenarios and async state changes
- Dart SDK:
>=2.17.0 <4.0.0 - Flutter:
>=3.20.0 - Uses Flutter hooks
^0.21.2
Releases are fully automated via GitHub Actions with OIDC authentication:
- Update version: Update version in
pubspec.yaml - Commit changes:
git commit -am "chore: bump version to v2.0.1" - Create tag:
git tag v2.0.1 - Push:
git push origin main --tags
Changelog is automatically generated from conventional commits.
When a tag matching v[0-9]+.[0-9]+.[0-9]+* is pushed:
- Environment Setup: Flutter, Dart, Bun, and Melos
- Dependencies: Install and bootstrap packages with Melos and Bun
- CI Validation: All tests, formatting, and analysis must pass
- Dry Run: Package publication tested with OIDC authentication
- Release Notes: Auto-generated using git-cliff from conventional commits
- GitHub Release: Created with generated release notes
- pub.dev Publication: Published automatically with OIDC (no token required)
Use Conventional Commits for automatic release note generation:
feat:- New featuresfix:- Bug fixesdocs:- Documentation changestest:- Test improvementsrefactor:- Code refactoringperf:- Performance improvements