Skip to content

Commit

Permalink
chore: setup nestjs app for E2E testing (#688)
Browse files Browse the repository at this point in the history
* chore: setup nestjs app for E2E testing

* chore: remove redundant logs
  • Loading branch information
Sky-FE committed Jul 3, 2024
1 parent 28aa7c4 commit 88a3a82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
39 changes: 18 additions & 21 deletions apps/nestjs-backend/test/utils/init-app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable sonarjs/no-duplicate-string */
import type { INestApplication } from '@nestjs/common';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { WsAdapter } from '@nestjs/platform-ws';
Expand Down Expand Up @@ -59,28 +58,25 @@ import { DevWsGateway } from '../../src/ws/ws.gateway.dev';
import { TestingLogger } from './testing-logger';

export async function initApp() {
let app: INestApplication<unknown>;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
if (globalThis.getApp) {
app = await globalThis.getApp();
} else {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
if (globalThis.initApp) return await globalThis.initApp();

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(NextService)
.useValue({
onModuleInit: () => {
return;
},
})
.overrideProvider(NextService)
.useValue({
onModuleInit: () => {
return;
},
})
.overrideProvider(DevWsGateway)
.useClass(WsGateway)
.compile();

app = moduleFixture.createNestApplication({
logger: new TestingLogger(),
});
}
.overrideProvider(DevWsGateway)
.useClass(WsGateway)
.compile();

const app = moduleFixture.createNestApplication({
logger: new TestingLogger(),
});

const configService = app.get(ConfigService);

Expand Down Expand Up @@ -120,6 +116,7 @@ export async function initApp() {
console.log(`> Test Ready on ${url}`);
console.log('> Test System Time Zone:', timeZone);
console.log('> Test Current System Time:', now.toString());

const sessionHandleService = app.get<SessionHandleService>(SessionHandleService);
return {
app,
Expand Down
9 changes: 8 additions & 1 deletion apps/nestjs-backend/vitest-e2e.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ interface ITestConfig {
baseId: string;
}

interface IInitAppReturnType {
app: INestApplication<unknown>;
appUrl: string;
cookie: string;
sessionID: string;
}

declare global {
// eslint-disable-next-line no-var
var testConfig: ITestConfig;
// eslint-disable-next-line no-var
var getApp: undefined | (() => Promise<INestApplication<unknown>>);
var initApp: undefined | (() => Promise<IInitAppReturnType>);
}

// Set global variables (if needed)
Expand Down

0 comments on commit 88a3a82

Please sign in to comment.