app.e2e.spec.tsβ’2.07 kB
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Test, TestingModule } from '@nestjs/testing'
import { INestApplication } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { createMockConfigService, setupTestEnvironment } from './test-config.helper'
import { AppController } from '../src/app/app.controller'
import { AppService } from '../src/app/app.service'
describe('AppController (e2e)', () => {
let app: INestApplication
beforeAll(async () => {
try {
// Set up environment variables BEFORE importing AppModule
// This ensures McpAuthModule.forRoot() and TimezoneModule.forRoot() have correct env vars
setupTestEnvironment()
// Dynamic import to ensure setupTestEnvironment() runs first
// (top-level imports are hoisted and run before module-level code)
// Only AppModule needs dynamic import as it triggers forRoot() calls
const { AppModule } = await import('../src/app/app.module')
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(ConfigService)
.useValue(createMockConfigService())
.compile()
app = moduleFixture.createNestApplication()
await app.init()
} catch (error) {
console.error('Failed to initialize app in tests:', error)
throw error
}
})
afterAll(async () => {
if (app) {
await app.close()
}
})
it('should be defined', () => {
expect(app).toBeDefined()
})
it('should have AppController', () => {
const appController = app.get(AppController)
expect(appController).toBeDefined()
})
it('should have AppService', () => {
const appService = app.get(AppService)
expect(appService).toBeDefined()
})
it('AppService.getHealth should return health status', () => {
const appService = app.get(AppService)
const result = appService.getHealth()
expect(result).toHaveProperty('status', 'ok')
expect(result).toHaveProperty('timestamp')
})
})