3.5 Hours

19. Testing Supabase Apps & CI/CD Pipelines

DevOps

Automated Testing & Deployment for Supabase Projects

A Supabase project without automated testing is a liability. This module covers pgTAP (Postgres unit testing for functions, RLS policies, and schema), end-to-end testing with the local stack, and CI/CD pipelines that run migrations safely.

Testing is essential for reliable software. With Supabase, you can test at multiple levels. pgTAP tests your database functions, triggers, and RLS policies directly in Postgres. Integration tests run against a local Supabase stack. End-to-end tests test your entire application. CI/CD pipelines automate testing and deployment. The local stack makes this possible — you can spin up a complete Supabase instance in CI. This catches issues before they reach production. The combination of pgTAP, integration tests, and CI/CD gives you confidence in your changes. You can deploy with confidence, knowing that your tests will catch regressions.

pgTAP: Unit Testing RLS Policies

pgTAP is a unit testing framework for PostgreSQL. It allows you to write tests for your database functions, triggers, and RLS policies. Tests are written in SQL and run inside a transaction that rolls back after each test. This ensures a clean state for every test. pgTAP provides assertion functions like is_empty, results_eq, and is. You can test RLS policies by setting the role and JWT claims in the test context. This verifies that policies work correctly. pgTAP tests run with supabase test db. They're essential for maintaining database integrity. Without pgTAP, you're flying blind — you don't know if your schema changes broke something. With pgTAP, you catch issues early.

SQL
-- Install pgTAP
CREATE EXTENSION IF NOT EXISTS pgtap;

-- Test file: supabase/tests/rls_posts_test.sql
BEGIN;
SELECT plan(6);

-- Set the role and JWT claims to simulate a user
SET LOCAL role = authenticated;
SET LOCAL request.jwt.claims = '{"sub": "user-1-uuid"}';

SELECT results_eq(
  $$ SELECT COUNT(*)::INT FROM posts WHERE user_id = 'user-1-uuid'::UUID $$,
  $$ VALUES (2) $$,
  'User 1 should see their own 2 posts'
);

SELECT finish();
ROLLBACK;

CI/CD with GitHub Actions

CI/CD pipelines automate testing and deployment. The Supabase CLI works in CI environments. The workflow: checkout the code, setup the Supabase CLI, start the local stack, apply migrations, run pgTAP tests, run integration tests, and finally deploy migrations to production. The deployment job runs only when tests pass and only on the main branch. This ensures that only tested changes reach production. The SUPABASE_ACCESS_TOKEN authenticates the CLI. The SUPABASE_PROJECT_ID identifies the target project. This workflow gives you confidence in your deployments.

Text
# .github/workflows/supabase.yml
name: Supabase CI
on: [pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Supabase CLI
        uses: supabase/setup-cli@v1
      - name: Start Supabase local stack
        run: supabase start
      - name: Apply migrations
        run: supabase db push
      - name: Run pgTAP tests
        run: supabase test db
  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - name: Deploy migrations to production
        run: supabase db push --linked

Integration Testing with Jest

Integration tests verify that your application code works correctly with the database. They run against the local Supabase stack. Use Jest or your preferred test framework. Create a client with the anon key. Test CRUD operations, RLS policies, and edge cases. For example, test that a user can only see their own posts. The local stack provides a clean, consistent environment for tests. Integration tests catch issues that unit tests miss — like correct RLS application, proper error handling, and API compatibility. They're an essential part of a robust testing strategy.

TypeScript
// tests/integration/posts.test.ts
import { createClient } from '@supabase/supabase-js';

const supabase = createClient(
  process.env.SUPABASE_URL!,
  process.env.SUPABASE_ANON_KEY!
);

describe('Posts RLS', () => {
  it('should only return current user posts', async () => {
    const { data, error } = await supabase
      .from('posts')
      .select('user_id');
    expect(error).toBeNull();
    expect(data?.every(p => p.user_id === user1.id)).toBe(true);
  });
});

Knowledge Check

Ready to test your understanding of 19. Testing Supabase Apps & CI/CD Pipelines?

Back