Skip to content

refactor: share the test helpers through supabase_common - #1745

Merged
spydon merged 1 commit into
mainfrom
sdk-1281-shared-test-helpers
Aug 20, 2026
Merged

spydon merged 1 commit into
mainfrom
sdk-1281-shared-test-helpers

Conversation

@spydon

@spydon spydon commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #1572 (SDK-1281): a duplication sweep after the extraction tiers landed showed that the remaining copy-paste lives almost entirely in the test suites. This PR grows supabase_common's existing testing.dart library with the helpers the suites kept re-implementing and moves the tests onto them.

New shared helpers

  • Session fixture (getSessionData, sessionDataUserId): was copied byte-for-byte in the supabase, supabase_flutter, and supabase_auth test utils.
  • Token endpoint fixtures (testUserJson, testSessionResponseJson, testUserId): the roughly 60-line signed-session response was duplicated verbatim between supabase_auth's RetryTestHttpClient and supabase_flutter's PkceHttpClient, down to the same signing key.
  • JWT builders (signedTestJwt, unsignedTestJwt): replace five hand-rolled builders across the auth, flutter, and realtime suites. The auth and flutter mock clients now sign with the shared HS256 helper instead of dart_jsonwebtoken, which lets supabase_flutter drop that dev dependency.
  • Mock HTTP client primitives (jsonStreamedResponse, JsonResponseMockClient, FailingHttpClient, stallUntilAborted): the JSON response builder existed in iceberg and auth, the 420 failing client in storage and auth, and the abort-trigger stalling block identically in the postgrest and supabase suites.
  • Realtime frames (postgresChangesFrame, todoColumns): the todos column fixture appeared eight times across the two mock WebSocket servers in supabase_realtime and supabase.

Cleanups along the way

  • supabase_flutter's MockAsyncStorage was a verbatim reimplementation of MemoryAuthAsyncStorage; it now extends it.
  • supabase_auth's single-purpose CustomHttpClient (420) is replaced by the shared FailingHttpClient.

No production code changes outside the supabase_common testing library, which is excluded from the capability-matrix scan.

Testing

  • dart analyze clean across all touched packages
  • Full test suites of all nine touched packages pass against the local CLI stack
  • Compliance symbol and drift checks pass locally

Summary by CodeRabbit

  • Refactor

    • Consolidated testing utilities for HTTP responses, session fixtures, JWTs, and Realtime event frames.
    • Removed duplicated test helpers and obsolete mock implementations across packages.
    • Simplified request-abort and retry test scenarios.
  • Tests

    • Preserved existing authentication, Realtime, database, storage, and function test coverage and behavior.
    • Improved consistency and maintainability of internal test infrastructure.
  • Chores

    • Removed unused testing dependencies and imports.

Extends the supabase_common testing library with the fixtures and mock
HTTP client primitives the package suites kept re-implementing: the
session fixture, the fixed test user and token endpoint response, JWT
builders (signed and unsigned), a JSON response mock client, the 420
failing client, an abort-aware stalling helper, and the realtime
postgres_changes frame builder with the todos column fixture. The
suites of supabase_auth, supabase, supabase_flutter, supabase_realtime,
supabase_storage, supabase_functions, postgrest, and iceberg now build
on those shared helpers, and supabase_flutter's MockAsyncStorage reuses
MemoryAuthAsyncStorage instead of reimplementing it, which also drops
the dart_jsonwebtoken dev dependency from supabase_flutter.
@spydon
spydon requested a review from a team as a code owner August 20, 2026 10:05
@coderabbitai

coderabbitai Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: a0b2ef3e-2656-48e2-a3ec-ceb7cfe7ea39

📥 Commits

Reviewing files that changed from the base of the PR and between dd61782 and ac26937.

📒 Files selected for processing (27)
  • packages/iceberg/test/iceberg_test.dart
  • packages/postgrest/test/retry_test.dart
  • packages/supabase/test/mock_test.dart
  • packages/supabase/test/postgrest_options_test.dart
  • packages/supabase/test/stream_filter_test.dart
  • packages/supabase/test/stream_integration_test.dart
  • packages/supabase/test/utils.dart
  • packages/supabase_auth/test/client_test.dart
  • packages/supabase_auth/test/custom_http_client.dart
  • packages/supabase_auth/test/mocks/json_response_mock_client.dart
  • packages/supabase_auth/test/redirect_url_mock_test.dart
  • packages/supabase_auth/test/src/set_session_test.dart
  • packages/supabase_auth/test/src/token_refresh_race_test.dart
  • packages/supabase_auth/test/utils.dart
  • packages/supabase_common/lib/src/testing/mock_http_clients.dart
  • packages/supabase_common/lib/src/testing/realtime_frames.dart
  • packages/supabase_common/lib/src/testing/session_fixture.dart
  • packages/supabase_common/lib/src/testing/test_jwt.dart
  • packages/supabase_common/lib/testing.dart
  • packages/supabase_flutter/pubspec.yaml
  • packages/supabase_flutter/test/utils.dart
  • packages/supabase_flutter/test/widget_test_stubs.dart
  • packages/supabase_functions/test/custom_http_client.dart
  • packages/supabase_realtime/test/mock_test.dart
  • packages/supabase_realtime/test/socket_test.dart
  • packages/supabase_realtime/test/utils/realtime_test_utils.dart
  • packages/supabase_storage/test/custom_http_client.dart
💤 Files with no reviewable changes (6)
  • packages/supabase_flutter/pubspec.yaml
  • packages/supabase_auth/test/redirect_url_mock_test.dart
  • packages/supabase/test/stream_integration_test.dart
  • packages/supabase/test/stream_filter_test.dart
  • packages/supabase_auth/test/mocks/json_response_mock_client.dart
  • packages/supabase_auth/test/utils.dart

Included review availability: Your plan provides up to 4 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The change centralizes HTTP, Realtime, session, and JWT test helpers in supabase_common/testing.dart. Package tests now use these shared helpers, and redundant local implementations and development dependencies were removed.

Changes

Shared testing infrastructure

Layer / File(s) Summary
Shared test helper foundation
packages/supabase_common/lib/src/testing/*, packages/supabase_common/lib/testing.dart
Added shared streamed-response, mock-client, abort, Realtime frame, session, and JWT helpers.
HTTP and Realtime test adoption
packages/iceberg/test/*, packages/postgrest/test/*, packages/supabase/test/*, packages/supabase_functions/test/*, packages/supabase_realtime/test/*, packages/supabase_storage/test/*
Replaced local response, abort, Realtime frame, and JWT implementations with shared helpers.
Authentication fixture adoption
packages/supabase_auth/test/*, packages/supabase_flutter/test/*, packages/supabase_flutter/pubspec.yaml
Replaced local authentication fixtures and storage mocks with shared utilities and removed the unused JWT development dependency.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to ac269

This change consolidates duplicated test helpers without changing production behavior, and the reported analysis, test, and compliance checks pass. No actionable merge-blocking risk remains beyond normal review.

Possibly related PRs

Suggested reviewers: tr00d

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: sharing duplicated test helpers through supabase_common.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sdk-1281-shared-test-helpers

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@spydon
spydon merged commit bcb41fa into main Aug 20, 2026
45 checks passed
@spydon
spydon deleted the sdk-1281-shared-test-helpers branch August 20, 2026 12:07
spydon added a commit that referenced this pull request Aug 21, 2026
Follow-up to #1745, which consolidated the test helpers of all the
client suites into `supabase_common`'s testing library. This PR breaks
those helpers out into a new published `supabase_testing` package aimed
at consumers, keeps `supabase_common` a purely internal package, and
adds the helpers and documentation that make testing an app against
Supabase easy.

## The package split

- `packages/supabase_testing` is a new published package holding the
customer-facing helpers: the mock HTTP clients, JWT builders, auth
fixtures and realtime frame builders from #1745. All nine client package
suites now consume them from there as a dev dependency.
- `supabase_common` goes back to its internal branding (README, pubspec
description and topics). Its `testing.dart` shrinks back to the
repo-internal local stack configuration, which is tied to this
repository's `supabase/config.toml` and is not useful to consumers.
- `supabase_testing` is excluded from the capability-matrix scan, since
test helpers are not SDK client surface.

## New helpers

Built on top of the moved primitives, verified by their own test suite:

- **`MockSupabaseHttpClient`**: answers requests from stubs registered
per endpoint, with shorthands for the service layouts so tests never
spell out URL paths: `stubTable` (`/rest/v1/<table>`), `stubRpc`,
`stubEdgeFunction` and `stubSignIn` (a working token endpoint). The
latest matching stub wins, `times` limits how often a stub answers so
call sequences can be modeled, and an unmatched request throws a
`StateError` naming the request and the registered stubs. Every answered
request is recorded with its body already read (`request.jsonBody`), so
tests can assert on what the code under test sent.
- **`testSupabaseClient`**: a `SupabaseClient` wired for tests, removing
the footguns of constructing one by hand: in-memory pkce storage, token
auto refresh off so no timer outlives the test, and an unsigned anon JWT
as the default API key.
- **`signInTestUser`**: puts a client into a signed-in state without any
network traffic (through `recoverSession` with generated fixtures), so
any test can run as an authenticated user with custom id, email, role
and claims.
- **`decodeTestJwtClaims`**: reads claims back out of a token for
assertions.

## Documentation

- The `supabase_testing` README is a full testing guide: quickstart,
stubbing each service, error and sequence stubbing, asserting on
recorded requests, running tests as a signed-in user, testing sign-in
flows, realtime frames, wiring `Supabase.initialize` in Flutter widget
tests, and when to reach for a real local stack instead.
- The root README gains a "Testing your app" section pointing at the
package.

Every code sample in the READMEs was executed as a real test before
being inlined, including the Flutter `Supabase.initialize` recipe.

## Testing

- New `supabase_testing` suite covers the mock client (matching
precedence, `times`, unmatched error, request recording, all
shorthands), the client factory and `signInTestUser`.
- Full suites of all ten touched packages pass against the local CLI
stack (1573 tests).
- `dart analyze` clean across the workspace, `dart format` reports no
changes, compliance symbol scan unaffected.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added the `supabase_testing` package with tools for mocking Supabase
requests, stubbing tables, RPCs, and Edge Functions, recording requests,
and testing authentication and realtime integrations.
* Added local test-client helpers, session fixtures, JWT utilities, and
reusable HTTP mocks.
* **Documentation**
* Added comprehensive setup and usage guidance for application testing.
* **Chores**
* Updated package testing configurations to use the new testing toolkit.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants