Browse Source

Refactor MongoDB setup for tests and improve utility functions

Yuki Takei 2 months ago
parent
commit
a93a85f782

+ 1 - 1
apps/app/test/setup/migrate-mongo.ts

@@ -1,7 +1,7 @@
 import { execSync } from 'node:child_process';
 import { beforeAll } from 'vitest';
 
-import { getTestDbConfig } from './mongo';
+import { getTestDbConfig } from './mongo/utils';
 
 // Track if migrations have been run for this worker
 let migrationsRun = false;

+ 2 - 35
apps/app/test/setup/mongo.ts → apps/app/test/setup/mongo/index.ts

@@ -1,45 +1,12 @@
-import ConnectionString from 'mongodb-connection-string-url';
 import { MongoMemoryServer } from 'mongodb-memory-server-core';
 import mongoose from 'mongoose';
 import { afterAll, beforeAll } from 'vitest';
 
 import { mongoOptions } from '~/server/util/mongoose-utils';
 
-let mongoServer: MongoMemoryServer | undefined;
-
-/**
- * Replace the database name in a MongoDB connection URI.
- * Uses mongodb-connection-string-url package for robust parsing.
- * Supports various URI formats including authentication, replica sets, and query parameters.
- *
- * @param uri - MongoDB connection URI
- * @param newDbName - New database name to use
- * @returns Modified URI with the new database name
- */
-export function replaceMongoDbName(uri: string, newDbName: string): string {
-  const cs = new ConnectionString(uri);
-  cs.pathname = `/${newDbName}`;
-  return cs.href;
-}
+import { getTestDbConfig } from './utils';
 
-/**
- * Get test database configuration for the current Vitest worker.
- * Each worker gets a unique database name to avoid conflicts in parallel execution.
- */
-export function getTestDbConfig(): {
-  workerId: string;
-  dbName: string;
-  mongoUri: string | null;
-} {
-  // VITEST_WORKER_ID is provided by Vitest (e.g., "1", "2", "3"...)
-  const workerId = process.env.VITEST_WORKER_ID || '1';
-  const dbName = `growi_test_${workerId}`;
-  const mongoUri = process.env.MONGO_URI
-    ? replaceMongoDbName(process.env.MONGO_URI, dbName)
-    : null;
-
-  return { workerId, dbName, mongoUri };
-}
+let mongoServer: MongoMemoryServer | undefined;
 
 beforeAll(async () => {
   // Skip if already connected (setupFiles run per test file, but connection persists per worker)

+ 1 - 1
apps/app/test/setup/mongo.spec.ts → apps/app/test/setup/mongo/utils.spec.ts

@@ -1,6 +1,6 @@
 import { describe, expect, it } from 'vitest';
 
-import { replaceMongoDbName } from './mongo';
+import { replaceMongoDbName } from './utils';
 
 describe('replaceMongoDbName', () => {
   describe('single-host URIs', () => {

+ 35 - 0
apps/app/test/setup/mongo/utils.ts

@@ -0,0 +1,35 @@
+import ConnectionString from 'mongodb-connection-string-url';
+
+/**
+ * Replace the database name in a MongoDB connection URI.
+ * Uses mongodb-connection-string-url package for robust parsing.
+ * Supports various URI formats including authentication, replica sets, and query parameters.
+ *
+ * @param uri - MongoDB connection URI
+ * @param newDbName - New database name to use
+ * @returns Modified URI with the new database name
+ */
+export function replaceMongoDbName(uri: string, newDbName: string): string {
+  const cs = new ConnectionString(uri);
+  cs.pathname = `/${newDbName}`;
+  return cs.href;
+}
+
+/**
+ * Get test database configuration for the current Vitest worker.
+ * Each worker gets a unique database name to avoid conflicts in parallel execution.
+ */
+export function getTestDbConfig(): {
+  workerId: string;
+  dbName: string;
+  mongoUri: string | null;
+} {
+  // VITEST_WORKER_ID is provided by Vitest (e.g., "1", "2", "3"...)
+  const workerId = process.env.VITEST_WORKER_ID || '1';
+  const dbName = `growi_test_${workerId}`;
+  const mongoUri = process.env.MONGO_URI
+    ? replaceMongoDbName(process.env.MONGO_URI, dbName)
+    : null;
+
+  return { workerId, dbName, mongoUri };
+}

+ 1 - 1
apps/app/vitest.workspace.mts

@@ -31,7 +31,7 @@ export default defineWorkspace([
       name: 'app-integration',
       environment: 'node',
       include: ['**/*.integ.ts'],
-      setupFiles: ['./test/setup/migrate-mongo.ts', './test/setup/mongo.ts'],
+      setupFiles: ['./test/setup/migrate-mongo.ts', './test/setup/mongo/index.ts'],
       deps: {
         // Transform inline modules (allows ESM in require context)
         interopDefault: true,