Learn
Test your navigation configuration
Run a complete test, distinguish commit from page completion and check stacks without rendering an app.
A Product page can open successfully and still return the wrong result, lose its retained tab or leave a private page behind after logout. These are different assertions. Start with one small test before checking the complete account flow.
1. Add the test companion
In an existing Flutter project, add these entries to the matching sections of pubspec.yaml, preserving the other dependencies:
dependencies:
ark_navigation: ^2.4.1
dev_dependencies:
ark_navigation_test: ^1.0.0
flutter_test:
sdk: flutter
Run flutter pub get. The companion belongs only in dev_dependencies. Your application continues to use Ark Navigation without importing the test package.
2. Create the complete test
Create test/navigation_lifecycle_test.dart with the following contents. It includes the Destination, Graph, Route and Mount; there are no omitted fixtures or generated files.
import 'package:ark_navigation/ark_navigation.dart';
import 'package:ark_navigation_test/ark_navigation_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test(
'commit opens an entry; pop later completes it with a typed result',
() async {
final NavigationTestDriver driver = NavigationTestDriver(
graph: _Fixture.graph(),
sessionId: const NavigationSessionId('example'),
);
addTearDown(driver.close);
final NavigationController navigation = driver.controller;
expect(
await NavigationTestWait.committed(
const _Page(1).on(navigation).push(),
timeout: const Duration(seconds: 1),
),
isA<NavigationCommitted>(),
);
final NavigationTicket<String> ticket = const _Page(
2,
).on(navigation).push();
final NavigationOperationResult opened =
await NavigationTestWait.committed(
ticket,
timeout: const Duration(seconds: 1),
);
expect(opened, isA<NavigationCommitted>());
final NavigationEntryId id = (opened as NavigationCommitted).entryId!;
expect(
await NavigationTestWait.operation(
navigation.pop(result: 'selected'),
timeout: const Duration(seconds: 1),
),
isA<NavigationCommitted>(),
);
final NavigationCompletion<String> closed =
await NavigationTestWait.completed(
ticket,
timeout: const Duration(seconds: 1),
);
expect(closed, isA<NavigationPopped<String>>());
expect((closed as NavigationPopped<String>).result, 'selected');
expect(
driver.observer,
NavigationTestMatchers.hasLifecycleExit(
sessionId: const NavigationSessionId('example'),
entryId: id,
reason: NavigationEntryExitReason.popped,
count: 1,
),
);
expect(driver.observer, NavigationTestMatchers.hasNoObservedFailures());
},
);
}
final class _Page extends NavigationDestination<String> {
const _Page(this.number);
final int number;
}
abstract final class _Fixture {
static NavigationGraph graph() => NavigationGraph(
rootScopeId: const NavigationScopeId('root'),
scopes: const <NavigationScopeDefinition>[
NavigationScopeDefinition(id: NavigationScopeId('root')),
],
modules: <NavigationModule>[
NavigationModule(
id: const NavigationModuleId('app'),
routes: <NavigationRouteDefinitionBase>[
NavigationRouteDefinition<_Page, String>(
id: const NavigationRouteId('page'),
restorationPolicy: NavigationRestorationPolicy.sessionOnly,
pageBuilder:
(
final BuildContext context,
final _Page destination,
final NavigationEntrySnapshot entry,
) => const SizedBox(),
),
],
),
],
mounts: <NavigationMount>[
NavigationMount(
id: const NavigationMountId('app'),
moduleId: const NavigationModuleId('app'),
scopeId: const NavigationScopeId('root'),
),
],
);
}
3. Run it and read the result
flutter test test/navigation_lifecycle_test.dart
Expect one passing test, not an application window. The first _Page remains in root. The second opens, returns “selected” when popped and produces exactly one exit with the popped reason. SizedBox deliberately avoids building product UI.
The two waits answer different questions:
| Wait | What it proves | What it does not prove |
|---|---|---|
| committed(ticket) | This request applied, rejected or failed | The page has closed or returned a result |
| completed(ticket) | This particular Entry finished, with its typed completion | A Flutter frame was rendered or an app resource finished closing |
| operation(future) | A stack operation, such as pop, settled | Every pending application task is idle |
In the test, _Page extends NavigationDestination<String>, so its ticket and popped result retain String typing. Entry ID comes from the real commit, not from a guessed route name.
Choose ownership before adding more assertions
NavigationTestDriver creates and owns a real Controller. Register driver.close immediately. It does not own the Graph's business services and does not create a Session or Widget tree.
For an already owned Session, attach NavigationTestObserver at construction. Pass its eventSink, failureSink and lifecycle observer separately; stopping observation must not dispose that Session. See the actual Flutter example.
Check identity, not just a route label
NavigationTestMatchers.hasStack checks a complete Snapshot: exact length and order. Supply mountId when one module has several placements, and entryId when the same retained page must survive a tab switch. A missing Scope is not an empty Scope.
hasLifecycleExit counts every exit of the specified Session/Entry and checks the reason of each. hasNoObservedFailures checks only the observed window; an intentional Guard rejection is not an unexpected failure.
Limits that matter in a failing test
- Each metadata channel retains 200 records by default. Dropped records, capture errors and detected Session mismatches are visible; exact observation assertions refuse incomplete evidence.
- A timeout stops waiting, not the source operation. Complete controlled Guard permissions explicitly, even when testing close during pending work.
- Flutter tests use fake time. Pump it explicitly; do not await a deadline that no test clock advances.
- didExit is not asynchronous resource completion. Await your own resource acknowledgement separately.
- Complete Snapshot assertions do not replace Finder assertions, native gesture tests or browser-history checks.
Continue with a real feature flow
- Catalog, login, retained tabs and logout: every helper is included.
- Observe a real Flutter host: actual Widgets, controlled Guard and fake time.
- Package reference: API map and versioned source.
- Detailed ownership contract.