Skip to content

Flaky config-migrate-api-url tests depend on module load order #489

Closed

Description

@neuromechanist

Symptom

test/config-migrate-api-url.test.ts passes on main HEAD (run 25861017229) but fails on dev HEAD (run 25861050894) with the same cumulative diff (just package.json version + auto-sync merge commit).

Failing assertions:

  • migrateApiUrl > first launch rewrites legacy URLs, drops top-level field, leaves live URLs alone
  • migrateApiUrl > only top-level field stale: accounts already clean, still drops the field

Root cause hypothesis

The test relies on NEMAR_CONFIG_DIR being set in beforeAll and being captured at module load. src/lib/config.ts reads it at module-level:

// Module-level state in src/lib/config.ts captures NEMAR_CONFIG_DIR at first
// import. Bun caches modules across tests, so we set up the config once,
// import once, and assert all migration scenarios from one snapshot.

If ANY earlier test in the same bun test invocation imports src/lib/config.ts (directly or transitively) before this test's beforeAll runs, the module-level capture sees a different (or unset) value. Subsequent tests see stale state.

Adding new test files (e.g. recent additions like backend/test/migrations.test.ts, backend/test/rate-limit-buckets.test.ts) may shift the load order enough to surface this latent bug.

Reproduce locally

bun test test/*.test.ts backend/test/*.test.ts | grep -A2 migrateApiUrl

May not reproduce on every run depending on dirent ordering / runner state.

Fix paths

  1. Refactor src/lib/config.ts to read NEMAR_CONFIG_DIR lazily (per function call) instead of at module load. This is the proper fix; removes the test-ordering hazard.
  2. Use Bun's --bail + per-file isolation so each test file gets a fresh module cache. Won't fix the root cause but stops cross-contamination.
  3. Drop the import-time singleton in the test by spawning the migration in a subprocess.

(1) is cleanest. The risk on (1) is recomputing the path each call, but the path is from an env var; this is cheap.

Why this is noise, not a regression

  • v0.8.14 already shipped to npm and is serving prod (api.nemar.org/health reports version 0.8.14)
  • Same code passes on main, fails on dev — diff is only the version field
  • The deploy pipeline ran cleanly: tag, npm publish, deploy backend, sync dev

Tracking for v0.8.15 cleanup.

Activity

  1. added
    bug
    testing
    on May 14, 2026
  2. added 2 commits that reference this issue on May 14, 2026
    82a3543
    f90be01
  3. neuromechanist commented on May 14, 2026

    @neuromechanist
    ContributorAuthor

    Merged via #490 to dev (commit f90be01).

  4. added a commit that references this issue on May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtesting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions