Skip to content

Testing

The backend uses three testing layers: Jest unit tests, Jest e2e suites, and live-DB rollback slices (*.e2e.ts in src/). The live-DB slices are the main integration tests because Kysely SQL is best verified against real PostgreSQL.

pnpm test runs Jest against src/**/*.spec.ts.

test/kysely-stub.cjs stubs the ESM-only kysely package so Jest can require it:

class Kysely {}
class PostgresDialect {}
const sql = () => ({ execute: async () => ({ rows: [] }) });
module.exports = { Kysely, PostgresDialect, sql };

Unit tests therefore focus on orchestration (services) and pure builders (PDF model builders), not actual SQL.

test/jest-e2e.json configures ts-jest with ESM stubs:

{
"testRegex": ".e2e-spec.ts$",
"moduleNameMapper": {
"^kysely$": "<rootDir>/kysely-stub.cjs",
"^puppeteer$": "<rootDir>/puppeteer-stub.cjs"
},
"setupFiles": ["<rootDir>/jest-e2e.setup.ts"]
}

test/jest-e2e.setup.ts disables cron and sets NODE_ENV=test.

  • test/app.e2e-spec.ts — boot AppModule, verify 401 on /api/v1, 404 on /
  • test/auth.e2e-spec.ts — full auth flow with in-memory repository overrides (login, me, RBAC denial, refresh rotation, logout denylist)

test/puppeteer-stub.cjs prevents Puppeteer from loading in Jest.

Each src/<module>/<module>.e2e.ts is a standalone script run by pnpm db:slice:<module>. They compile a Nest testing module, run operations inside a scope.audit(...) transaction that throws a rollback-marker, then assert the transaction is rolled back.

Terminal window
pnpm db:slice:core
pnpm db:slice:client
pnpm db:slice:admin
pnpm db:slice:finances
pnpm db:slice:facturation
pnpm db:slice:market
pnpm db:slice:pressing
pnpm db:slice:restaurant
pnpm db:slice:salle-fete
pnpm db:slice:rh
pnpm db:slice:audit
pnpm db:slice:uploads

A typical slice:

  1. Boot Test.createTestingModule([...]).
  2. Seed test data inside one scope.audit(actor, async (a, tx) => { ... }).
  3. Read results and assert audit attribution.
  4. Throw new Error('rollback-marker').
  5. Catch the marker and verify nothing persisted.

For example, admin/admin.e2e.ts verifies:

  • mot_de_passe is not returned by repository reads
  • audit.journal_audit rows either contain *** or do not contain the plaintext password
  • audit rows are attributed to the acting user
  • the test user no longer exists after rollback

pnpm db:smoke runs src/db/smoke.ts, which exercises the generated Database type and basic connectivity against a live PostgreSQL instance.

Terminal window
pnpm db:verify

This is the recommended command after any schema or backend change. It migrates the sibling database, regenerates types, runs tsc --noEmit, unit tests, smoke tests, and live-DB slices.