# Backend testing

## Running

```bash
npm test            # all 14 suites, ~3s on M-series Mac with parallel workers
npm run test:watch  # re-runs on file change
npm run test:cov    # with coverage report
```

The current pass count is **91 tests across 14 suites**. CI gates on this.

## Node version

Pin to **Node 22 LTS** (`brew install node@22 && brew link --force --overwrite node@22`).
We tried Node 25.x for two days and hit a steady stream of webpack-bundled CJS
module incompatibilities — silent `tsc` exits, jest exporting empty modules,
`pg-pool` and `@nestjs/platform-express` failing to load, Playwright's
`coreBundle.js` erroring on init, `next dev` crashing inside `webpack.js`.
None of these are project bugs; all were Node-25-specific. Until the ecosystem
catches up, stay on 22.

CI must enforce this. `engines.node` in package.json declares ">=20.0.0" — fine
for now, but consider tightening to "22.x" when other repos in the org pin too.

## Test layout

- `src/**/*.spec.ts` — unit tests, mostly service-level with mocked deps.
- `tests/e2e/` (repo root) — Playwright suite that boots the real backend
  + 3 portals + ephemeral Postgres. This is the contract-level test gate
  and runs cold via `npm test` from `tests/e2e/`.

## Adding tests

When a service grows new branches, add a spec next to it (`foo.service.ts`
→ `foo.service.spec.ts`). Mock the repositories via `getRepositoryToken`,
mock the cross-module deps via `useValue`. See `appointments.service.spec.ts`
for the canonical pattern.

Don't import the real `DataSource` from unit tests — that's the e2e
suite's job. Unit tests should run without Docker.
