test(server): new test facilities (#10870)

close CLOUD-142
This commit is contained in:
forehalo
2025-03-17 10:02:12 +00:00
parent 92db9a693a
commit 9b5d12dc71
12 changed files with 311 additions and 46 deletions
@@ -0,0 +1,5 @@
import { app, e2e } from './test';
e2e('should create test app correctly', async t => {
t.truthy(app);
});
@@ -0,0 +1,79 @@
import { INestApplication } from '@nestjs/common';
import { NestApplication } from '@nestjs/core';
import { Test, TestingModuleBuilder } from '@nestjs/testing';
import cookieParser from 'cookie-parser';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import {
AFFiNELogger,
CacheInterceptor,
CloudThrottlerGuard,
GlobalExceptionFilter,
OneMB,
} from '../../base';
import { SocketIoAdapter } from '../../base/websocket';
import { AuthGuard } from '../../core/auth';
import { TEST_LOG_LEVEL } from '../utils';
interface TestingAppMetadata {
tapModule?(m: TestingModuleBuilder): void;
tapApp?(app: INestApplication): void;
}
export class TestingApp extends NestApplication {
async [Symbol.asyncDispose]() {
await this.close();
}
}
export async function createApp(
metadata: TestingAppMetadata = {}
): Promise<TestingApp> {
const { buildAppModule } = await import('../../app.module');
const { tapModule, tapApp } = metadata;
const builder = Test.createTestingModule({
imports: [buildAppModule()],
});
// when custom override happens
if (tapModule) {
tapModule(builder);
}
const module = await builder.compile();
module.useCustomApplicationConstructor(TestingApp);
const app = module.createNestApplication<TestingApp>({
cors: true,
bodyParser: true,
rawBody: true,
});
const logger = new AFFiNELogger();
logger.setLogLevels([TEST_LOG_LEVEL]);
app.useLogger(logger);
app.use(cookieParser());
app.useBodyParser('raw', { limit: 1 * OneMB });
app.use(
graphqlUploadExpress({
maxFileSize: 10 * OneMB,
maxFiles: 5,
})
);
app.useGlobalGuards(app.get(AuthGuard), app.get(CloudThrottlerGuard));
app.useGlobalInterceptors(app.get(CacheInterceptor));
app.useGlobalFilters(new GlobalExceptionFilter(app.getHttpAdapter()));
const adapter = new SocketIoAdapter(app);
app.useWebSocketAdapter(adapter);
app.enableShutdownHooks();
if (tapApp) {
tapApp(app);
}
return app;
}
@@ -0,0 +1,4 @@
import { createApp } from './create-app';
// @ts-expect-error testing
globalThis.app = await createApp();
@@ -0,0 +1,9 @@
import test, { registerCompletionHandler } from 'ava';
export const e2e = test;
// @ts-expect-error created in prelude.ts
export const app = globalThis.app;
registerCompletionHandler(() => {
app.close();
});