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
RealtimeClientas its WebSocket transport, so channels andstream()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
jwtwithout 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
Responsewithbodyencoded as UTF-8 JSON and a matching content type, for aMockSupabaseHttpClienthandler to return. -
jsonStreamedResponse(
Object? body, {int statusCode = 200, BaseRequest? request, Map< String, String> headers = const {}, String? reasonPhrase}) → StreamedResponse -
A
StreamedResponsewithbodyencoded 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 asLifecycleRule.fromJsonreads it. -
postgresChangesFrame(
Object? topic, {required List< int> ids, required Map<String, dynamic> data}) → String -
A protocol 2.0.0
postgres_changestext 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 withsecret. -
signInTestUser(
AuthClient auth, {String userId = testUserId, String email = 'fake1@email.com', String role = 'authenticated', Map< String, dynamic> claims = const {}, DateTime? expiresAt}) → Future<Session> -
Puts
authinto 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 asFileObject.fromJsonreads it. -
testSessionResponseJson(
{required String accessToken, String refreshToken = 'tDoDnvj5MKLuZOQ65KyVfQ', Map< String, dynamic> ? user}) → Map<String, dynamic> -
The JSON form of a token endpoint response carrying
accessTokenanduser, 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
SupabaseClientwired 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
claimsdirectly.
Typedefs
-
StubHandler
= FutureOr<
Response> Function(RecordedRequest request) - Builds the response a MockSupabaseHttpClient answers a matched request with, from the request it received.