initializeTestSupabase function
- Client? httpClient,
- String url = 'http://localhost:54321',
- String? publishableKey,
- Map<
String, String> ? headers, - WebSocketTransport? realtimeTransport,
- AuthAsyncStorage? asyncStorage,
- bool autoRefreshToken = false,
- SupabaseRetryOptions retryOptions = const SupabaseRetryOptions(enabled: false),
Initializes Supabase for a test and returns the instance.
Compared to Supabase.initialize, this keeps the session and the pkce
code verifier in memory through asyncStorage, which defaults to a fresh
MemoryAuthAsyncStorage, turns off the token auto refresh so no timer
outlives the test, disables deep link detection so no platform channel is
touched, turns off the retries of database requests and token refreshes so
a stubbed failure surfaces at once, and defaults publishableKey to
testPublishableKey. Pass retryOptions to exercise retry handling. Pass a
MockSupabaseHttpClient as httpClient to answer the requests, and the
call method of a MockRealtimeTransport as realtimeTransport to
answer the realtime traffic, the same way testSupabaseClient takes them.
Dispose the instance when the test ends, for example with
addTearDown(supabase.dispose), so the next test can initialize again.
Throws a StateError when an earlier test left Supabase initialized,
since that instance still holds the earlier test's mocks.
Implementation
@visibleForTesting
Future<Supabase> initializeTestSupabase({
Client? httpClient,
String url = 'http://localhost:54321',
String? publishableKey,
Map<String, String>? headers,
WebSocketTransport? realtimeTransport,
AuthAsyncStorage? asyncStorage,
bool autoRefreshToken = false,
SupabaseRetryOptions retryOptions = const SupabaseRetryOptions(
enabled: false,
),
}) {
if (Supabase.isInitializingOrInitialized) {
return Future.error(
StateError(
'Supabase is already initialized. Dispose it at the end of every '
'test, for example with addTearDown(supabase.dispose).',
),
);
}
return Supabase.initialize(
url: url,
publishableKey: publishableKey ?? testPublishableKey,
httpClient: httpClient,
headers: headers,
realtimeOptions: RealtimeClientOptions(transport: realtimeTransport),
postgrestOptions: PostgrestClientOptions(retryOptions: retryOptions),
authOptions: FlutterAuthClientOptions(
autoRefreshToken: autoRefreshToken,
retryOptions: retryOptions,
asyncStorage: asyncStorage ?? MemoryAuthAsyncStorage(),
detectSessionInUri: false,
),
);
}