Browse Source

Connection checks in test setup

Yuki Takei 2 months ago
parent
commit
ca2586d34e
2 changed files with 14 additions and 0 deletions
  1. 9 0
      apps/app/test/setup/migrate-mongo.ts
  2. 5 0
      apps/app/test/setup/mongo.ts

+ 9 - 0
apps/app/test/setup/migrate-mongo.ts

@@ -5,6 +5,9 @@ import { mongoOptions } from '~/server/util/mongoose-utils';
 
 import { getTestDbConfig } from './mongo';
 
+// Track if migrations have been run for this worker
+let migrationsRun = false;
+
 /**
  * Run database migrations using migrate-mongo API.
  * This is necessary when using external MongoDB in CI to ensure each worker's
@@ -42,6 +45,11 @@ async function runMigrations(mongoUri: string, dbName: string): Promise<void> {
 }
 
 beforeAll(async () => {
+  // Skip if already run (setupFiles run per test file, but we only need to migrate once per worker)
+  if (migrationsRun) {
+    return;
+  }
+
   const { dbName, mongoUri } = getTestDbConfig();
 
   // Only run migrations when using external MongoDB (CI environment)
@@ -53,4 +61,5 @@ beforeAll(async () => {
   console.log(`Running migrations for ${dbName}...`);
 
   await runMigrations(mongoUri, dbName);
+  migrationsRun = true;
 });

+ 5 - 0
apps/app/test/setup/mongo.ts

@@ -42,6 +42,11 @@ export function getTestDbConfig(): {
 }
 
 beforeAll(async () => {
+  // Skip if already connected (setupFiles run per test file, but connection persists per worker)
+  if (mongoose.connection.readyState === 1) {
+    return;
+  }
+
   const { workerId, dbName, mongoUri } = getTestDbConfig();
 
   // Use external MongoDB if MONGO_URI is provided (e.g., in CI with GitHub Actions services)