Просмотр исходного кода

Merge pull request #10747 from growilabs/support/improve-test-parallelism

support: Improve test parallelism
Yuki Takei 2 месяцев назад
Родитель
Сommit
217b3a75fb

+ 1 - 1
AGENTS.md

@@ -128,7 +128,7 @@ Or from apps/app directory:
 ```bash
 pnpm run lint:typecheck
 pnpm run lint:biome
-pnpm run test:vitest
+pnpm run test
 pnpm run build
 ```
 

+ 2 - 2
apps/app/AGENTS.md

@@ -25,7 +25,7 @@ pnpm run dev:migrate            # Run database migrations
 # Quality Checks
 pnpm run lint:typecheck         # TypeScript type check
 pnpm run lint:biome             # Biome linter
-pnpm run test:vitest            # Run tests
+pnpm run test            # Run tests
 
 # Build
 pnpm run build                  # Build for production
@@ -128,7 +128,7 @@ import { Button } from '~/components/Button';
 ```bash
 pnpm run lint:typecheck   # 1. Type check
 pnpm run lint:biome       # 2. Lint
-pnpm run test:vitest      # 3. Run tests
+pnpm run test             # 3. Run tests
 pnpm run build            # 4. Verify build (optional)
 ```
 

+ 5 - 4
apps/app/package.json

@@ -34,10 +34,11 @@
     "lint": "run-p lint:**",
     "prelint:openapi:apiv3": "pnpm run openapi:generate-spec:apiv3",
     "prelint:openapi:apiv1": "pnpm run openapi:generate-spec:apiv1",
-    "test": "pnpm run test:vitest:coverage",
-    "test:vitest": "vitest run",
-    "test:vitest:coverage": "COLUMNS=200 vitest run --coverage",
-    "reg:run": "reg-suit run",
+    "test": "vitest run",
+    "test:coverage": "run-p test:coverage:* test:integ",
+    "test:coverage:unit": "COLUMNS=200 vitest run --coverage --project=app-unit",
+    "test:coverage:components": "COLUMNS=200 vitest run --coverage --project=app-components",
+    "test:integ": "vitest run --project=app-integration",
     "//// misc": "",
     "console": "npm run repl",
     "repl": "cross-env NODE_ENV=development npm run ts-node src/server/repl.ts",

+ 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 };
+}

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

@@ -31,7 +31,10 @@ 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,

+ 1 - 1
lefthook.yml

@@ -5,6 +5,6 @@ pre-commit:
   parallel: true
   commands:
     biome-format:
-      glob: "*.{js,jsx,ts,tsx,json,jsonc}"
+      glob: "*.{js,jsx,ts,tsx,mts,cts,json,jsonc}"
       run: pnpm biome check --write --no-errors-on-unmatched --files-ignore-unknown=true {staged_files}
       stage_fixed: true