briqbriq
Guides

Run tests against real services

Replace mocks with a real Postgres and Redis for the duration of a test run.

Mocks drift. Testcontainers needs Docker on the machine running the tests, which agents in cloud sandboxes and CI runners often do not have. briq gives the test run real services for a few cents and tears them down after.

From the SDK

test/global-setup.ts
import { Briq } from '@briq/sdk';

const briq = new Briq(process.env.BRIQ_API_KEY);

export async function setup() {
  const [db, cache] = await Promise.all([
    briq.run({ recipe: 'postgres', stack: 'ci', ttlMinutes: 30 }),
    briq.run({ recipe: 'redis', stack: 'ci', ttlMinutes: 30 }),
  ]);
  process.env.DATABASE_URL = db.outputs.DATABASE_URL;
  process.env.REDIS_URL = cache.outputs.REDIS_URL;
  return async () => {
    await Promise.all([briq.destroy(db.id), briq.destroy(cache.id)]);
  };
}

Register it as a Vitest globalSetup. The ttlMinutes: 30 is a safety net: if the runner is killed before teardown, the briqs stop on their own.

From an agent

Run the integration tests against a real Postgres and Redis instead of the mocks.

The agent uses the postgres and redis recipes, exports the outputs, runs pnpm test:integration, and destroys both briqs. Because tool results carry the cost line, it can also tell you what the run cost.

Running the tests inside briq

If the code under test must reach the services by their internal hosts (db.ci.internal), run the tests in a briq too:

briq_run({ image: "node:22", stack: "ci", cmd: ["sleep", "infinity"], size: "small" })
briq_files_put({ briq: "bq_…", path: "/app.tgz", content_base64: "…" })
briq_exec({ briq: "bq_…", cmd: ["sh", "-c", "tar xzf /app.tgz -C / && cd /app && pnpm test"] })

Output over 32 KB is truncated in briq_exec; read the rest with briq_logs.

Keep it cheap

  • nano is enough for Redis and for most test databases.
  • Reuse a stack across runs during a session; destroy it at the end.
  • Use a bq_test_… key against the mock provider to test the wiring itself without machines.