This backend has two separate, independent test setups, for two different jobs:
- Jest + Supertest (
tests/*.test.tsandtests/smoke/*.test.ts): fast, in-process, against a real local PostgreSQL. This is whatpnpm --filter karmacircle-api testruns. - A standalone e2e server (
tests/e2e/server.ts): a real, listening instance of this API that exists soapps/web's Playwright suite has something to drive a browser against. Jest never uses this, and Playwright never uses Jest's harness. See apps/web's testing spec for how the two fit together.
Moved to PostgreSQL on 2026-11-29. The Jest suite passed on PostgreSQL in CI, in the production API deploy from
mainon 2026-11-29. No run of the e2e server or of Playwright is recorded since the move.
It needs a local PostgreSQL server, and never uses the development database.
By default it uses postgresql://localhost:5432/karmacircle_test; set TEST_DATABASE_URL for another one.
The database is created and migrated before the first file (tests/helpers/global.setup.ts, test-database.ts), every table is emptied before the first test of a file and after each test (jest.setup.ts), and files run one at a time because they share it.
prepareTestDatabase refuses any database that is not local and named *_test or *_e2e.
tests/helpers/env.setup.ts stubs every required env var, so no real secrets are needed, and leaves WORKER_URL empty.
The rate limiters and daily budgets are skipped when NODE_ENV is test.
Tests build the app with buildTestApp() (tests/helpers/test-app.ts) and drive it with Supertest.
Shared helpers:
tests/helpers/records.tsdoes what the API cannot: seeds an event in the past, reads or changes a stored row, and lists the queued emails by kind (emailsOf,addressesOf). Event email is asserted on thejobstable, not on a mailer mock.tests/helpers/events.tssigns people up and calls the event routes.
Mock only external services (razorpay, resend, Vercel Blob), not the database.
| Area | Files |
|---|---|
| Accounts | auth, users, public-accounts |
| Organizations and payments | organizations, payments |
| Events | events and the events-* files: access and waitlist, attendees, capacity, concurrency, details, directory, draft rules, drafts, emails, hybrid, link privacy, manage, ongoing, timezone, types |
| Other modules | uploads |
| Shared pieces | mailer (rendering and password reset), health, rate-limit-store, database-gate, logger |
| Short end-to-end flows | smoke/auth, smoke/events, smoke/registrations |
Per-file counts are not kept here; run Jest to see them.
event-indexes.test.ts (MongoDB query plans) and cover-migration.test.ts (a one-off move of covers only old MongoDB events had) are left in the folder, excluded in jest.config.js, and no longer compile.
Cases about records saved before a field existed were removed with the port, because the PostgreSQL schema cannot hold such a record.
buildTestApp() returns a server bound to 127.0.0.1 (November 2026).
Handing Supertest the bare Express app makes it listen on the wildcard address and connect to 127.0.0.1, where another local process can hold the same port, which showed up as random foreign responses.
The helper listens on 127.0.0.1 itself and closes in afterAll.
payments.test.ts's Razorpay mock and its singleton gotcha. payment.service.ts caches its Razorpay client in a module variable, so a mocked constructor runs once per test file. Keep one stable ordersCreate = jest.fn() and have MockedRazorpay.mockImplementation() return it, because clearMocks: true wipes mock.results between tests while the cached client lives on.
A real, listening instance of this API, started only by Playwright (apps/web/playwright.config.ts's webServer array) or manually via pnpm --filter karmacircle-api run test:e2e-server. It exists because a real browser driven by Playwright makes real HTTP requests to a real URL — Jest's in-process Supertest approach has nothing to bind to.
-
Backed by its own PostgreSQL database (
karmacircle_e2e, orE2E_DATABASE_URL), created, migrated and emptied when it starts. It needs a local PostgreSQL server. -
A dedicated port (5051 by default), deliberately different from local dev's 5050 - see tests/e2e/env.setup.ts. This process must be able to run alongside a developer's real
pnpm devsession without colliding with it or touching its data.ORIGIN_URLis likewise overridden tohttp://localhost:3001(apps/web's own dedicated e2e port - seeapps/web/e2e/env.ts), so CORS actually allows the e2e web server's origin rather than local dev's:3000. -
A test-only reset route.
createApp()(../../src/app.ts) registersPOST /__test__/reset- empties every table but onlyif (env.NODE_ENV === "test"), so it can never exist in a real deployment. Playwright'sapps/web/e2e/global-setup.tscalls it once before the whole suite runs, so the suite starts from an empty database. It isn't called between individual spec files - see theapps/webtesting spec for why (parallelism, and the unique-data-per-run pattern that avoids needing it). -
A non-production password-reset outbox. config/mailer.ts never calls Resend unless
env.NODE_ENV === "production". Development and test push{ to, resetUrl }into an in-memory array instead.GET /__test__/last-reset-url?email=...reads the most recent entry in tests, and loopback-onlyGET /__dev__/last-reset-url?email=...does the same in local development.POST /__test__/resetclears the outbox too (clearResetOutbox()), alongside wiping the database. This lets both Playwright and local developers complete password-reset flows without a real mail provider. -
A test-only events router, since November 2026.
server.tsis now an outer Express app that mounts the router from tests/e2e/test-routes.ts and thencreateApp(), so these routes sit in front of the real app and never exist in it. The first route isPOST /__test__/events/:uid/backdate(body{ daysAgo }, a positive number, default 1): it setsstartTimetodaysAgodays before now,endTimetwo hours later, andstartDate/endDateto the matching UTC day starts, returning204,400for a baddaysAgo, or404for an unknown uid. It exists because the API refuses to create a past event or join a started one; browser tests publish a future event, optionally join it, then backdate it. Registrations are not touched. The second,POST /__test__/events/:uid/fundraiser, gives an event a fixed sample fundraiser (204, or404), because the API routes cannot set one. The Jest suite does not use either.
If you change what createApp() needs at boot (a new required env var, a new startup side effect), check both tests/helpers/env.setup.ts (Jest) and tests/e2e/env.setup.ts (Playwright's server) - they're meant to carry the same placeholder values (the e2e one literally imports the Jest one and sets its own PORT, ORIGIN_URL, DATABASE_URL and UPLOADS_DIR on top), but nothing enforces that beyond this note.
If you add or move a test file, update the table above. If you change anything about how the e2e server boots (port, env, the reset route), update this file and apps/web's testing.md in the same change - they describe two ends of one integration and are easy to let drift apart.