From f573c869ba7f7ee3d21b8a3add2c7eb8b39403c5 Mon Sep 17 00:00:00 2001 From: Anuraj Jit Saikia Date: Thu, 22 Oct 2026 13:21:38 +0530 Subject: [PATCH] fix(server): download mongod once before test workers start On a cold cache every Mongo-backed suite started the mongod download inside its own beforeAll. Parallel workers contended for the same lock and the download outlived the 10s hook timeout, so Server tests failed at random on fresh CI runners. A vitest globalSetup now fetches the binary once first. --- server/test/global-setup.js | 15 +++++++++++++++ server/test/helpers/mongo.js | 5 +++-- server/vitest.config.js | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 server/test/global-setup.js diff --git a/server/test/global-setup.js b/server/test/global-setup.js new file mode 100644 index 0000000..53d9399 --- /dev/null +++ b/server/test/global-setup.js @@ -0,0 +1,15 @@ +import { MongoBinary } from 'mongodb-memory-server'; + +/** + * Fetch the `mongod` binary once, before any test file runs. + * + * On a cold cache (every fresh CI runner) each suite that calls + * `useMongoMemoryServer` would otherwise start the ~100 MB download inside its + * own `beforeAll`, in parallel workers that contend for the same lock file. + * The download outlives the 10 s hook timeout, so those suites fail at random + * depending on network speed. Global setup has no hook timeout and runs before + * the workers start, so the suites only ever find a warm cache. + */ +export default async function setup() { + await MongoBinary.getPath(); +} diff --git a/server/test/helpers/mongo.js b/server/test/helpers/mongo.js index 83abe4f..09cbce4 100644 --- a/server/test/helpers/mongo.js +++ b/server/test/helpers/mongo.js @@ -18,8 +18,9 @@ import { MongoMemoryServer } from 'mongodb-memory-server'; * `mongod` binary into its cache (~/.cache/mongodb-binaries) from * fastdl.mongodb.org, then reuses it on later runs. That fetch is a *runtime* * step performed by the test, not an npm postinstall — so it is unaffected by - * CI's `npm ci --ignore-scripts` and keeps installs fast. The first CI run on - * a cold cache pays the one-time download. + * CI's `npm ci --ignore-scripts` and keeps installs fast. `test/global-setup.js` + * performs that download once before the workers start, so this hook never + * races other suites for it. */ export function useMongoMemoryServer() { let mongod; diff --git a/server/vitest.config.js b/server/vitest.config.js index 56c6168..a92f4ed 100644 --- a/server/vitest.config.js +++ b/server/vitest.config.js @@ -3,6 +3,7 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { environment: 'node', + globalSetup: ['./test/global-setup.js'], setupFiles: ['./test/setup.js'], include: ['test/**/*.test.js'], coverage: {