db-integration-tests

Write bun:test integration tests for Kysely queries against a real local PostgreSQL database.

7|12|Updated Mar 26, 2026
One-click install
npx skills add https://github.com/OpenRouterTeam/docs --skill db-integration-tests-openrouterteam
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: db-integration-tests
Source: https://github.com/OpenRouterTeam/docs/tree/main/.agents/skills/db-integration-tests
Command: npx skills add https://github.com/OpenRouterTeam/docs --skill db-integration-tests-openrouterteam

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing database tests that mock the query layer proves nothing about the SQL that actually runs and silently rots as the schema changes. This Skill provides the conventions, templates, and mandatory patterns for writing DB integration tests that hit a real local PostgreSQL instance, ensuring query logic, foreign key constraints, and data transformations are genuinely validated. ## Core Features & Use Cases - Standardized test structure: Enforces bun:test framework, domain-based file organization under packages/db/integration/, and read/write test separation. - Fixture lifecycle rules: Mandates beforeAll/afterAll fixture management with tracked cleanup, AggregateError teardown, and random nonces for test data isolation. - Coverage verification: Guides running the coverage script and reading lcov.info to confirm every exported query function is exercised. - Banned pattern enforcement: Prohibits DB mocking, vitest, raw SQL, and silent error swallowing. - Use Case: After adding a new query function to packages/db, use this Skill to write an integration test that inserts fixtures, calls the query via dbRead/dbWrite, asserts on the Result type, and verifies coverage shows the function executed. ## Quick Start Write an integration test for the new query function in packages/db following the db-integration-tests conventions and verify it appears in the coverage report.

Frequently Asked Questions about db-integration-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write database integration tests with bun:test and Kysely?▼

Place tests in packages/db/integration/<domain>/ using bun:test, query through dbRead/dbWrite or createDBRequestContextForIntegrationTest(), create fixtures in beforeAll, and clean up in afterAll with aggregated error handling. Run with bun run test:integration from packages/db.

How to run a single focused integration test file with bun?▼

Pass a filter through the package script, for example bun run test:integration -- --test-name-pattern='...'. Appending a file path to the script is not focused; for one file, invoke bun test <path> directly with the same preload and integration environment.

Can I mock the database in DB query tests?▼

No. Mocking dbRead/dbWrite, Kysely, or module-mocking queries.ts files is prohibited because it proves nothing about the actual SQL. All new DB tests must be integration tests against real PostgreSQL or the Spanner emulator suite.

Why does my integration test fail with database context not initialized?▼

This happens when invoking bun test directly without the preload script. The preload-integration.ts file sets OR_ENV=test and calls setupDbIntegrationContext(); run tests via bun run test:integration or include the preload explicitly.

How do I verify test coverage for exported query functions?▼

Run bun run test:integration:coverage in packages/db, then inspect coverage/lcov.info for your query file. FNDA:0 entries indicate functions never called; files missing from the report entirely mean no test imports them.

Why does my insert fail with duplicate key value violates unique constraint?▼

The table's id sequence likely lags behind max(id) due to seed or manual inserts with explicit ids. Fix it with setval('<seq>', (select max(id) from <table>)) and rerun; note some sequences keep legacy names like categories_* for the tags table.