Write tests
Add the tests a change needs, at the right layer, and keep the package above its coverage floor. Test layers lists what each layer is for and where it lives.
Add a unit test
- Create
tests/<thing>.test.tsin the package. Keep it free of the database and the network; stub collaborators. - Run it:
pnpm --filter <package> test
Add an integration test
Use this for anything that reads or writes the database. Docker must be running.
- If the package has no
vitest.integration.config.ts, copypackages/calendar/vitest.integration.config.ts, add@monark/test-utilsas a dev dependency, and add the scripts to the package'spackage.json:"test:integration": "vitest run --config vitest.integration.config.ts --passWithNoTests", "test:integration:coverage": "vitest run --config vitest.integration.config.ts --coverage --passWithNoTests" - Create
tests/integration/<thing>.test.ts. Clear the tables you touch before each test:import { getDb } from "@monark/db"; import { truncate } from "@monark/test-utils/db"; beforeEach(async () => { await truncate(getDb(), ["Post"]); }); - Run it:
Each run starts a fresh Postgres container and applies every migration first.pnpm --filter <package> test:integration
Keep coverage above the floor
- Run both layers with coverage, then merge:
pnpm --filter <package> test:coverage pnpm --filter <package> test:integration:coverage pnpm coverage:merge - For a new package, add a floor for it to
THRESHOLDSintools/merge-coverage.ts.pnpm coverage:suggestprints one based on your run.
Good to know:
- Run integration specs only through their config. From an IDE without it, a guard stops the run so it can't write to your development database.
- Don't mock the database; a real container catches query mistakes a mock hides.
- Don't lower a floor to get a green build. Add the missing tests, or explain the change in the pull request.
Next: Add a search source