Skip to content

Fix flaky tests: share one Postgres container with per-suite schemas #70

@marcelosalloum

Description

@marcelosalloum

Problem

CI test runs are intermittently failing with Exceeded timeout of 15000 ms for a hook (e.g. PR #69, which passed only on re-run with no code changes).

The failures are not assertion failures — they are the testcontainers Postgres beforeAll hook timing out.

Root cause

tests/setup.ts is registered via setupFilesAfterEnv in jest.config.ts, so it runs once per test file. With 6 test suites, that means:

  • 6 separate Postgres containers are started and stopped per CI run
  • each runs prisma db push in its beforeAll
  • each must complete within the global testTimeout: 15000 (15s)

On CI runners (slower IO, image pull, cold start) the per-suite container startup + prisma db push regularly exceeds 15s, causing the whole suite's tests to fail. Locally it's non-deterministic (one run: 2 suites fail on the hook; next run: fully green).

Proposed fix (option 1: single container + per-suite schema)

Start one Postgres container for the whole test run and isolate suites by Postgres schema:

  1. Move container startup into a Jest globalSetup (and teardown into globalTeardown) so the container is created/destroyed once per run.
  2. Give each test file its own Postgres schema (e.g. derived from the worker id / file name) and set search_path per suite, or use a separate database per worker.
  3. Run prisma db push once against the shared instance, or once per schema as needed.

Benefits: container started once (faster, far fewer cold-start timeouts) while preserving test isolation between suites.

Alternatives considered

  • Bump the hook timeout (beforeAll(async () => {…}, 60000)) — lowest effort, keeps per-suite isolation, but keeps the 6x container churn (slow) and only masks the race.
  • Shared container + truncate/rollback between tests — works but more bookkeeping; risk of cross-suite state leakage if not careful with parallel workers.

Acceptance criteria

  • Single Postgres container per test run (not per suite)
  • Test isolation preserved (no cross-suite state leakage)
  • CI test step is reliably green across repeated runs
  • Test suite wall-clock time does not regress (ideally improves)

References

  • tests/setup.ts
  • jest.config.ts (setupFilesAfterEnv, testTimeout: 15000)
  • Surfaced during PR fix: patch dependabot security alerts #69 (dependency security fixes), where CI failed on flakiness then passed on re-run.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Backlog (Not Ready)

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions