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: {