supabase_test library

Test helpers for apps and packages built on the Supabase clients.

Holds the mock HTTP client, JWT builders, auth fixtures and realtime frames an app needs to test code that talks to Supabase without a running stack. The test suites of the client packages themselves run on the same primitives, and keep the fixtures only they need in package:supabase_test/internal.dart.

Everything here is annotated with @visibleForTesting, so the analyzer warns when a helper leaks outside of test code.

Classes

MockRealtimeTransport
A realtime server in memory, plugged into a RealtimeClient as its WebSocket transport, so channels and stream() can be tested without a running stack.
MockSupabaseHttpClient
A mock HTTP client that answers Supabase requests from stubs registered per endpoint, and records every request it answered in requests.
RecordedRequest
A request a MockSupabaseHttpClient has answered, with its body already read, so a test can assert on what the code under test sent.

Constants

testUserId → const String
Id of the user testUserJson describes.

Functions

decodeTestJwtClaims(String jwt) → Map<String, dynamic>
Decodes the claims of jwt without verifying its signature, for asserting on tokens the code under test produced or sent.
jsonResponse(Object? body, {int statusCode = 200, Map<String, String> headers = const {}}) → Response
A Response with body encoded as UTF-8 JSON and a matching content type, for a MockSupabaseHttpClient handler to return.
jsonStreamedResponse(Object? body, {int statusCode = 200, BaseRequest? request, Map<String, String> headers = const {}, String? reasonPhrase}) → StreamedResponse
A StreamedResponse with body encoded as JSON and a matching content type.
lifecycleRuleJson({String id = 'expire-history', String status = 'Enabled', int noncurrentDays = 30, int? newerNoncurrentVersions}) → Map<String, dynamic>
The JSON form of a lifecycle rule expiring noncurrent object versions after noncurrentDays, as the storage API stores it and as LifecycleRule.fromJson reads it.
postgresChangesFrame(Object? topic, {required List<int> ids, required Map<String, dynamic> data}) → String
A protocol 2.0.0 postgres_changes text frame, which is a positional array of [join_ref, ref, topic, event, payload], encoded as JSON.
signedTestJwt(Map<String, dynamic> claims, {required String secret}) → String
Crafts an HS256 JWT carrying exactly claims, signed with secret.
signInTestUser(AuthClient auth, {String userId = testUserId, String email = 'fake1@email.com', String role = 'authenticated', Map<String, dynamic> claims = const {}, DateTime? expiresAt}) → Future<Session>
Puts auth into a signed-in state without any network traffic, and returns the resulting session.
storageObjectJson(String name, {String id = 'e7b5c1d2-4c7a-4f3e-9c1a-2b3d4e5f6a7b', String bucketId = 'test-bucket', String? owner, Map<String, dynamic>? metadata, DateTime? createdAt, DateTime? updatedAt, String? version, DateTime? archivedAt, bool? isDeleteMarker, bool? isVersioned}) → Map<String, dynamic>
The JSON form of a storage object named name, as the storage API lists it and as FileObject.fromJson reads it.
testSessionResponseJson({required String accessToken, String refreshToken = 'tDoDnvj5MKLuZOQ65KyVfQ', Map<String, dynamic>? user}) → Map<String, dynamic>
The JSON form of a token endpoint response carrying accessToken and user, which defaults to testUserJson.
testSupabaseClient({Client? httpClient, MockRealtimeTransport? realtime, String url = 'http://localhost:54321', String? apiKey, bool autoRefreshToken = false, List<SupabaseClientPlugin> plugins = const []}) → SupabaseClient
A SupabaseClient wired for tests.
testUserJson({String id = testUserId, String email = 'fake1@email.com', String phone = '166600000000'}) → Map<String, dynamic>
The JSON form of a fixed test user carrying an unverified TOTP factor and an email identity, as the auth server would return it.
unsignedTestJwt(Map<String, dynamic> claims) → String
Crafts a JWT by base64url-encoding claims directly.

Typedefs

StubHandler = FutureOr<Response> Function(RecordedRequest request)
Builds the response a MockSupabaseHttpClient answers a matched request with, from the request it received.