Skip to content

Flaky test: store.test.ts "returns undefined before any config is set" depends on file execution order under Bun #374

@MattDevy

Description

@MattDevy

Summary

test/config/store.test.ts contains a test that is order-dependent and intermittently fails under Bun's test runner:

1 tests failed:
(fail) store > getResolvedConfig > returns undefined before any config is set [2.72ms]

Observed in CI run 26566675678 / job 78263017234 on PR #373. A re-run of the same commit (26566675678 second attempt) passed, confirming the failure is non-deterministic.

Root cause

test/config/store.test.ts:24 asserts:

it('returns undefined before any config is set', () => {
  assert.equal(getResolvedConfig(), undefined)
})

The file's only cleanup hook is afterEach, which runs after each test. The first test therefore relies on the module's initial state (_config = undefined at module load time).

  • Under Node's built-in --test runner, each test file runs in an isolated worker, so the singleton state is always fresh.
  • Under Bun's runner, all test files share a single process and the same module instance. Whenever another test file (test/lib/cloud-client.test.ts, test/lib/es-client.test.ts, test/lib/kibana-client.test.ts, test/kb/kibana-client.test.ts, test/factory.test.ts) loads first and calls setResolvedConfig, its afterEach is supposed to reset state — but file ordering, parallelism, and missing before cleanup combine to expose a race where _config is non-undefined when the store test starts.

Small changes to other test files (adding/removing tests, renaming describes) can shift Bun's file-discovery order and trigger the failure. This makes it a latent CI hazard that surfaces on unrelated PRs.

Suggested fix

Add an explicit reset before the first test so it's order-independent:

import { describe, it, beforeEach, afterEach } from 'node:test'
// ...

beforeEach(() => {
  setResolvedConfig(undefined as unknown as ResolvedConfig)
})

afterEach(() => {
  setResolvedConfig(undefined as unknown as ResolvedConfig)
})

Optionally rename the first test from "returns undefined before any config is set" → "returns undefined when the store has been reset" so the assertion matches what's actually being verified.

The same pattern (relying on initial singleton state) should be audited in other test files that import from src/config/store.ts to make sure none of them have a similar order-dependent first test.

Acceptance criteria

  • test/config/store.test.ts passes regardless of which test files run before it.
  • CI is green on both the Node and Bun runners after the fix is merged.
  • Documented in the PR description that the fix is for a Bun-specific cross-file isolation gap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions