pglite-test

Postgres Tests Without a Server

A drop-in pgsql-test backed by an in-process PGlite instance. Instant, isolated Postgres databases with per-test rollbacks and pgpm-native seeding — no server, no Docker, no CI service container.

Zero Infrastructure

Pure WASM — no Postgres server, no Docker, no service container

Drop-in pgsql-test

Same getConnections() API; your tests read exactly the same

Per-test Rollback

Every test runs in its own transaction or savepoint

pglite-test Features

Run your whole Postgres test suite in-process

In-Process PGlite

One isolated instance per suite on ElectricSQL's WASM build of Postgres — no createdb, no psql, no TCP.

Ref-counted Rollbacks

The standard two-client beforeEach/afterEach harness emits exactly one BEGIN/SAVEPOINT/ROLLBACK per test on the shared session.

RLS-friendly

Role-based auth via setContext() with full GUC support; the standard app roles are bootstrapped for you by default.

pgpm-native Seeding

seed.pgpm() deploys your real database modules into PGlite with the unmodified pgpm engine.

WASM Extensions

Register pgvector, pg_trgm and friends at construction, then CREATE EXTENSION as usual.

Auto Teardown

In-memory by default: no residue, no reboots, just clean exits — ideal for GitHub Actions.

ts
import { getConnections, PgTestClient } from 'pglite-test';

let pg: PgTestClient;
let db: PgTestClient;
let teardown: () => Promise<void>;

beforeAll(async () => {
  ({ pg, db, teardown } = await getConnections());
});

afterAll(() => teardown());

beforeEach(async () => {
  await pg.beforeEach();
  await db.beforeEach();
});

afterEach(async () => {
  await db.afterEach();
  await pg.afterEach();
});

it('queries the pgpm-deployed schema', async () => {
  const { rows } = await db.query('SELECT count(*)::int AS n FROM app.users');
  expect(rows[0].n).toBe(0);
});

The Same Tests, No Server

The getConnections() API mirrors pgsql-test, so existing suites port by changing the import. Each suite gets its own in-process PGlite instance with automatic rollbacks.

ts
import { getConnections, seed } from 'pglite-test';
import { vector } from '@electric-sql/pglite-pgvector';

const { db, teardown } = await getConnections(
  {
    pglite: {
      dataDir: undefined,           // in-memory by default
      extensions: { vector },       // WASM extensions
      extensionSql: [
        'CREATE EXTENSION IF NOT EXISTS vector;'
      ]
    }
  },
  [seed.pgpm()]                     // deploy real modules with the pgpm engine
);

WASM Extensions and Options

Register WASM extensions like pgvector at construction, keep data in memory or on disk, and seed with the unmodified pgpm engine.

ts
await getConnections({
  pglite: {
    roles: false, // skip the built-in role bootstrap
    extensionSql: [
      "CREATE ROLE app_reader NOLOGIN;",
      "CREATE ROLE app_writer NOLOGIN;",
      "CREATE ROLE app_user LOGIN;",
      "GRANT app_writer TO app_user;"
    ]
  }
});

Bring Your Own Roles

The standard app roles are created for you by default. Opt out for a clean superuser-only instance and define your own roles with real Postgres DDL.