main.spec.tsβ’4.67 kB
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder } from '@nestjs/swagger'
import { setupTestEnvironment } from '../test/test-config.helper'
// Mock dependencies
vi.mock('@nestjs/core')
vi.mock('@nestjs/swagger', async (importOriginal) => {
const actual = await importOriginal<typeof import('@nestjs/swagger')>()
const mockDocumentBuilder = {
setTitle: vi.fn().mockReturnThis(),
setDescription: vi.fn().mockReturnThis(),
setVersion: vi.fn().mockReturnThis(),
addServer: vi.fn().mockReturnThis(),
addTag: vi.fn().mockReturnThis(),
addBearerAuth: vi.fn().mockReturnThis(),
addBasicAuth: vi.fn().mockReturnThis(),
build: vi.fn().mockReturnValue({}),
}
return {
...actual,
DocumentBuilder: vi.fn(() => mockDocumentBuilder),
SwaggerModule: {
createDocument: vi.fn(),
setup: vi.fn(),
},
}
})
// Set up test environment before running tests
setupTestEnvironment()
describe('main.ts bootstrap', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let mockApp: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let mockContext: any
let originalEnv: Record<string, string | undefined>
beforeEach(() => {
// Save original env
originalEnv = { ...process.env }
// Create mock Reflector
const mockReflector = {
getAllAndOverride: vi.fn(),
}
// Create mock app with auth-related methods
mockApp = {
listen: vi.fn().mockResolvedValue(undefined),
getUrl: vi.fn().mockResolvedValue('http://localhost:3000'),
close: vi.fn().mockResolvedValue(undefined),
get: vi.fn().mockReturnValue(mockReflector), // For app.get(Reflector)
useGlobalGuards: vi.fn(), // For app.useGlobalGuards()
}
// Create mock context
mockContext = {
close: vi.fn().mockResolvedValue(undefined),
}
})
afterEach(() => {
// Restore environment
process.env = originalEnv
vi.clearAllMocks()
vi.resetModules()
})
it('should bootstrap in HTTP mode when MCP_TRANSPORT is not stdio', async () => {
delete process.env.MCP_TRANSPORT
delete process.env.PORT // Ensure PORT fallback is tested
vi.mocked(NestFactory.create).mockResolvedValue(mockApp)
// Import and run bootstrap (need to import after mocks are set up)
const { bootstrap } = await import('./main.test-helper')
await bootstrap()
expect(NestFactory.create).toHaveBeenCalled()
expect(mockApp.listen).toHaveBeenCalledWith(3000)
})
it('should use custom PORT in HTTP mode', async () => {
process.env.PORT = '8080'
delete process.env.MCP_TRANSPORT
vi.mocked(NestFactory.create).mockResolvedValue(mockApp)
const { bootstrap } = await import('./main.test-helper')
await bootstrap()
expect(mockApp.listen).toHaveBeenCalledWith(8080)
})
it('should bootstrap in STDIO mode when MCP_TRANSPORT is stdio', async () => {
process.env.MCP_TRANSPORT = 'stdio'
vi.mocked(NestFactory.createApplicationContext).mockResolvedValue(mockContext)
const { bootstrap } = await import('./main.test-helper')
await bootstrap()
expect(NestFactory.createApplicationContext).toHaveBeenCalledWith(expect.anything(), {
logger: false,
})
expect(NestFactory.create).not.toHaveBeenCalled()
})
it('should configure Swagger with correct settings in HTTP mode', async () => {
delete process.env.MCP_TRANSPORT
vi.mocked(NestFactory.create).mockResolvedValue(mockApp)
const { bootstrap } = await import('./main.test-helper')
await bootstrap()
// Verify DocumentBuilder was instantiated and configured
expect(DocumentBuilder).toHaveBeenCalled()
})
it('should default to development mode when NODE_ENV is not set', async () => {
delete process.env.NODE_ENV
delete process.env.MCP_TRANSPORT
vi.mocked(NestFactory.create).mockResolvedValue(mockApp)
const { bootstrap } = await import('./main.test-helper')
await bootstrap()
// Verify Swagger is configured (only happens in development)
expect(DocumentBuilder).toHaveBeenCalled()
})
it('should not configure Swagger in production mode', async () => {
process.env.NODE_ENV = 'production'
delete process.env.MCP_TRANSPORT
vi.mocked(NestFactory.create).mockResolvedValue(mockApp)
vi.clearAllMocks() // Clear previous DocumentBuilder calls
const { bootstrap } = await import('./main.test-helper')
await bootstrap()
// Verify Swagger is NOT configured in production
expect(DocumentBuilder).not.toHaveBeenCalled()
})
})