Skip to content

ConfigCache module-level setInterval causes intermittent "Cannot log after tests are done" under Jest #797

Description

@bendogabriel

Problem

.aiox-core/core/config/config-cache.js starts a module-level setInterval (60s sweep) at require-time. The timer is unref()'d (so it doesn't hang the process), but under Jest it keeps firing after a test file's suite finishes and after Jest tears down the test environment. When the sweep lands mid-teardown with AIOX_DEBUG logging (or any future log in that callback), Jest throws:

Cannot log after tests are done

The failure is intermittent — it depends on whether the 60s sweep lands inside a suite's window — which makes it a classic flaky-CI source. We hit it on a downstream fork's CI (run 28661638857-equivalent) and had to patch locally.

Fix that works (running on our fork)

Skip the timer entirely in Jest workers — JEST_WORKER_ID is set by every Jest worker process; CLI/runtime processes are unaffected:

if (!process.env.JEST_WORKER_ID) {
  const cacheCleanupTimer = setInterval(() => {
    const cleared = globalConfigCache.clearExpired();
    if (cleared > 0 && process.env.AIOX_DEBUG) {
      console.log(`🗑️ Config cache: Cleared ${cleared} expired entries`);
    }
  }, 60 * 1000);

  if (typeof cacheCleanupTimer.unref === 'function') {
    cacheCleanupTimer.unref();
  }
}

Happy to send this as a PR if useful.

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    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