-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(@nestjs/typeorm): add test for connectionFactory
Add unit test for async options with connectionFactory.
- Loading branch information
1 parent
6295073
commit a9ef5df
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
tests/e2e/typeorm-async-connection-factory-options.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} |