import { INestApplication, ValidationPipe } from "@nestjs/common"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { Test, TestingModule } from "@nestjs/testing"; import { TypeOrmModule } from "@nestjs/typeorm"; import { ConfigModule } from "@nestjs/config"; import type { NestExpressApplication } from "@nestjs/platform-express"; import { ScheduleModule } from "@nestjs/schedule"; import { configureBodyParsers } from "../src/common/body-parsers"; import { dataSourceOptions } from "../src/database/data-source"; import { ImportModule } from "../src/import/import.module"; jest.mock("@nestjs/common", () => { const actual = jest.requireActual("@nestjs/common"); // eslint-disable-next-line @typescript-eslint/no-require-imports const log = require("debug")("test"); // eslint-disable-next-line @typescript-eslint/no-require-imports const { Logger } = require("@nestjs/common/services/logger.service"); Logger.prototype.error = function ( message: unknown, stack?: string, context?: string, ) { const ctx = context ?? this.context ?? "App"; log(`[${ctx}]`, "error", message, ...(stack ? [stack] : [])); }; for (const level of ["log", "warn", "debug", "verbose", "fatal"] as const) { Logger.prototype[level] = function (message: unknown, context?: string) { const ctx = context ?? this.context ?? "App"; log(`[${ctx}]`, level, message); }; } return actual; }); import { BrowserModule } from "../src/browser/browser.module"; import { SessionModule } from "../src/session/session.module"; import { EnvironmentModule } from "../src/environment/environment.module"; import { ScenarioModule } from "../src/scenario/scenario.module"; import { McpModule } from "../src/mcp/mcp.module"; import { HealthController } from "../src/health/health.controller"; import { ManifestController } from "../src/manifest/manifest.controller"; import { HttpExceptionFilter } from "../src/filters/http-exception.filter"; import { LoggingInterceptor } from "../src/interceptors/logging.interceptor"; import { validateAppConfig } from "../src/config/app.config"; import { AuthModule } from "../src/auth/auth.module"; export async function buildTestApp( databasePath = ":memory:", ): Promise { // Each test app stores files in its own temp dir: suites run in parallel and // some clean up their files dir, which must not affect other suites. const filesDir = fs.mkdtempSync(path.join(os.tmpdir(), "liqa-test-files-")); process.env.FILES_DIR = filesDir; const module: TestingModule = await Test.createTestingModule({ imports: [ ConfigModule.forRoot({ isGlobal: true, ignoreEnvFile: true, validate: validateAppConfig, }), // The real migrations, not synchronize, so tests use the schema production gets TypeOrmModule.forRoot({ ...dataSourceOptions(databasePath), migrationsRun: true, }), ScheduleModule.forRoot(), BrowserModule, AuthModule, SessionModule, EnvironmentModule, ScenarioModule, ImportModule, McpModule, ], controllers: [HealthController, ManifestController], }).compile(); const app = module.createNestApplication(); configureBodyParsers(app); app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true })); app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalInterceptors(new LoggingInterceptor()); await app.init(); const close = app.close.bind(app); app.close = async () => { await close(); fs.rmSync(filesDir, { recursive: true, force: true }); }; return app; }