Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 7.58 KB

testing.md

File metadata and controls

77 lines (55 loc) · 7.58 KB

Testing

This backend has two separate, independent test setups, for two different jobs:

  1. Jest + Supertest (tests/*.test.ts and tests/smoke/*.test.ts): fast, in-process, against a real local PostgreSQL. This is what pnpm --filter karmacircle-api test runs.
  2. A standalone e2e server (tests/e2e/server.ts): a real, listening instance of this API that exists so apps/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 main on 2026-11-29. No run of the e2e server or of Playwright is recorded since the move.

Jest + Supertest

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.ts does 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 the jobs table, not on a mailer mock.
  • tests/helpers/events.ts signs people up and calls the event routes.

Mock only external services (razorpay, resend, Vercel Blob), not the database.

Coverage

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.

The e2e server (tests/e2e/server.ts)

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, or E2E_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 dev session without colliding with it or touching its data. ORIGIN_URL is likewise overridden to http://localhost:3001 (apps/web's own dedicated e2e port - see apps/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) registers POST /__test__/reset - empties every table but only if (env.NODE_ENV === "test"), so it can never exist in a real deployment. Playwright's apps/web/e2e/global-setup.ts calls it once before the whole suite runs, so the suite starts from an empty database. It isn't called between individual spec files - see the apps/web testing 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-only GET /__dev__/last-reset-url?email=... does the same in local development. POST /__test__/reset clears 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.ts is now an outer Express app that mounts the router from tests/e2e/test-routes.ts and then createApp(), so these routes sit in front of the real app and never exist in it. The first route is POST /__test__/events/:uid/backdate (body { daysAgo }, a positive number, default 1): it sets startTime to daysAgo days before now, endTime two hours later, and startDate/endDate to the matching UTC day starts, returning 204, 400 for a bad daysAgo, or 404 for 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, or 404), 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.

Keeping this file honest

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.