engineTest function
- @visibleForTesting
- @isTest
Defines a single test that runs with a dedicated Engine and TestClient.
This helper ensures that the test callback is executed with full provider
support, providing the correct context for routed operations.
description: The description of the test.callback: The async function containing the test logic. It receives theEngineandTestClientinstances.engine: An optional existingEngineinstance to use. If not provided, a new one is created with default providers.client: An optional existingTestClientinstance to use. If not provided, a new one is created based ontransportMode.transportMode: The transport mode for theTestClient(defaults toinMemory).configItems: Initial configuration items for theEngineif a new one is created. These are passed toCoreServiceProvider.engineConfig: AnEngineConfiginstance for theEngineif a new one is created.options: A list ofEngineOptfor theEngineif a new one is created.providers: Custom providers to use instead of default providers.autoCloseEngine: Close the provided engine after the test finishes. Engines created by this helper are always closed automatically.
Implementation
@visibleForTesting
@isTest
void engineTest(
String description,
TestCallback callback, {
Engine? engine,
TransportMode transportMode = TransportMode.inMemory,
Map<String, dynamic>? configItems,
EngineConfig? engineConfig,
List<EngineOpt>? options,
List<ServiceProvider>? providers,
bool autoCloseEngine = false,
}) {
test_package.test(description, () async {
final ownsEngine = engine == null;
final shouldCloseEngine = ownsEngine || autoCloseEngine;
final testEngine =
engine ??
_createTestEngine(
configItems: configItems,
engineConfig: engineConfig,
options: options,
providers: providers,
);
final handler = RoutedRequestHandler(testEngine);
final client = transportMode == TransportMode.inMemory
? TestClient.inMemory(handler)
: TestClient.ephemeralServer(handler);
try {
// TEMP: avoid AppZone wrapping to surface zone dependencies.
await callback(testEngine, client);
} finally {
await client.close();
if (shouldCloseEngine) {
// Ensure providers are booted before cleanup so provider teardown
// hooks can resolve their dependencies.
await testEngine.initialize();
await testEngine.close();
}
}
});
}