Skip to content

Commit

Permalink
Export functions from main.ts and create test code
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejkang committed Aug 11, 2021
1 parent 914dfab commit eba6cf7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
66 changes: 66 additions & 0 deletions src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { NestFactory } from '@nestjs/core';
import { INestApplication } from '@nestjs/common';
import { SwaggerModule, OpenAPIObject, SwaggerCustomOptions } from '@nestjs/swagger';

jest.mock('@nestjs/core');

interface DocumentationConfig {
title: string;
description: string;
version: string;
endpoint: string;
}

describe('Main', () => {
it('should creates a NestFactory', async () => {
const listenSpy = jest.fn();
const createSpy = Promise.resolve({
listen: listenSpy,
}) as unknown as Promise<INestApplication>;

jest.spyOn(NestFactory, 'create').mockImplementation(() => createSpy);

const bootstrap = require('./main').bootstrap;
await bootstrap();
expect(listenSpy).toHaveBeenCalledTimes(2);
expect(listenSpy).toHaveBeenCalledWith(3000);
});

it('should correctly initialize swagger API documentation', () => {
const nestApp = {};
const swaggerDocument = {};
SwaggerModule.createDocument = jest.fn().mockReturnValue(swaggerDocument);
SwaggerModule.setup = jest.fn();

const documentConfig: DocumentationConfig = {
title: 'Driver License Verification API',
description: 'An unofficial driver license verification crawler API service with live status check support.',
version: 'Alpha',
endpoint: 'docs',
};

const customConfig: SwaggerCustomOptions = {
customCss: `
.topbar { display: none; }
.swagger-ui { padding-top: 50px; }`,
customSiteTitle: 'Driver License Verification API',
};

const setupDocument = require('./main').setupDocument;

setupDocument(nestApp);

expect(SwaggerModule.createDocument).toHaveBeenCalledWith(
nestApp,
expect.objectContaining(<Omit<OpenAPIObject, 'paths'>>{
info: {
contact: {},
description: documentConfig.description,
title: documentConfig.title,
version: documentConfig.version,
},
}),
);
expect(SwaggerModule.setup).toHaveBeenCalledWith(documentConfig.endpoint, nestApp, swaggerDocument, customConfig);
});
});
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
export async function bootstrap() {
const app = await NestFactory.create(AppModule);

setupDocument(app);

await app.listen(3000);
}

async function setupDocument(app: INestApplication) {
export async function setupDocument(app: INestApplication) {
const config = new DocumentBuilder()
.setTitle('Driver License Verification API')
.setDescription('An unofficial driver license verification crawler API service with live status check support.')
Expand Down

0 comments on commit eba6cf7

Please sign in to comment.