{
  "id": 14929688,
  "title": "Setting an env var above your imports does not run before them, and our integration test works anyway",
  "url": "https://urgent.news/2026/10/16/setting-an-env-var-above-your-imports-does-not-run-before-them-and",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-16T10:30:57.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/daniel_pertu/setting-an-env-var-above-your-imports-does-not-run-before-them-and-our-integration-test-works-fko"
  },
  "original_language": "en",
  "account": "The largest test file in PubTrivia, containing 543 lines, calls actual server actions, the engine, and the Postgres database against which it operates. This setup has real costs associated with it. The key insight came from the two lines placed above the imports that disable WebSocket notifications before importing modules that read them during execution. Despite the belief that these lines would run before the imported modules, this is not the case in an ES module environment. Import declarations are hoisted, and all imported modules are fully evaluated before the first statement of the importing module runs. Consequently, the order of assignments and imports does not matter, and the test functions correctly for a different reason. The code being disabled is an async function that notifies a WebSocket server with session ID, event, and payload. The function checks if the required variables are set before proceeding. However, when the variables are blanked before the imports, the function returns early with a warning, ensuring the test is the only one controlling the closing of a question. This demonstrates the importance of understanding the evaluation order of modules in ES environments and the impact it can have on test results.",
  "summary": "The largest test file in PubTrivia is 543 lines, and it has no mocks in it. It calls the actual server actions a player's phone calls, and the actual engine the host's buttons and our WebSocket server both call, against the actual Postgres database the app uses. That is a trade with real costs, which I will get to. First, the two lines at the top, because they taught me something about module…",
  "key_points": [
    "Lines above imports do not run before imports in ES modules",
    "Import declarations are hoisted, evaluated before module execution",
    "Async function disabling WebSocket notifications returns early with warning"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}