From a9ef5dfd812af8c297eff6aca1e6c3b9cc77ffe7 Mon Sep 17 00:00:00 2001 From: IRCraziestTaxi Date: Wed, 7 Jul 2021 18:20:06 -0500 Subject: [PATCH] test(@nestjs/typeorm): add test for connectionFactory Add unit test for async options with connectionFactory. --- ...m-async-connection-factory-options.spec.ts | 30 ++++++++++++ ...async-connection-factory-options.module.ts | 47 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tests/e2e/typeorm-async-connection-factory-options.spec.ts create mode 100644 tests/src/async-connection-factory-options.module.ts diff --git a/tests/e2e/typeorm-async-connection-factory-options.spec.ts b/tests/e2e/typeorm-async-connection-factory-options.spec.ts new file mode 100644 index 000000000..e345b9518 --- /dev/null +++ b/tests/e2e/typeorm-async-connection-factory-options.spec.ts @@ -0,0 +1,30 @@ +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import * as request from 'supertest'; +import { AsyncConnectionFactoryOptionsFactoryModule } from '../src/async-connection-factory-options.module'; +import { Server } from 'http'; + +describe('TypeOrm (async configuration with connectionFactory)', () => { + let server: Server; + let app: INestApplication; + + beforeEach(async () => { + const module = await Test.createTestingModule({ + imports: [AsyncConnectionFactoryOptionsFactoryModule], + }).compile(); + + app = module.createNestApplication(); + server = app.getHttpServer(); + await app.init(); + }); + + it(`should return created entity`, () => { + return request(server) + .post('/photo') + .expect(201, { name: 'Nest', description: 'Is great!', views: 6000 }); + }); + + afterEach(async () => { + await app.close(); + }); +}); diff --git a/tests/src/async-connection-factory-options.module.ts b/tests/src/async-connection-factory-options.module.ts new file mode 100644 index 000000000..09eaa5064 --- /dev/null +++ b/tests/src/async-connection-factory-options.module.ts @@ -0,0 +1,47 @@ +import { Module } from '@nestjs/common'; +import { createConnection } from 'typeorm'; +import { TypeOrmModule } from '../../lib'; +import { Photo } from './photo/photo.entity'; +import { PhotoModule } from './photo/photo.module'; + +@Module({ + imports: [ + TypeOrmModule.forRootAsync({ + useFactory: () => ({ + type: 'postgres', + host: '0.0.0.0', + port: 3306, + username: 'root', + password: 'root', + database: 'test', + entities: [Photo], + synchronize: true, + retryAttempts: 2, + retryDelay: 1000, + }), + connectionFactory: async (options) => { + // Realistically, this function would be used for more than simply creating a connection, + // i.e. checking for an existing and active connection prior to creating a new one. + // However, including that logic here causes runtime test errors about variables being used before assignment. + // Therefore, given the simple nature of this test case, simply create and return a connection. + const connection = await createConnection(options!); + return connection; + }, + }), + TypeOrmModule.forRoot({ + name: 'connection_2', + type: 'postgres', + host: '0.0.0.0', + port: 3306, + username: 'root', + password: 'root', + database: 'test', + entities: [Photo], + synchronize: true, + retryAttempts: 2, + retryDelay: 1000, + }), + PhotoModule, + ], +}) +export class AsyncConnectionFactoryOptionsFactoryModule {}