38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import 'reflect-metadata';
|
|
import { DatabaseService } from '../src/database/database.service';
|
|
import { parseEnvironment } from '../src/config/environment';
|
|
|
|
describe('database lifecycle', () => {
|
|
let database: DatabaseService;
|
|
beforeEach(() => {
|
|
database = new DatabaseService(
|
|
parseEnvironment({ DATABASE_URL: 'postgresql://localhost/mani' }),
|
|
);
|
|
});
|
|
it('connects during initialization', async () => {
|
|
const connect = jest.spyOn(database, '$connect').mockResolvedValue();
|
|
await database.onModuleInit();
|
|
expect(connect).toHaveBeenCalledTimes(1);
|
|
});
|
|
it('disconnects during shutdown', async () => {
|
|
const disconnect = jest.spyOn(database, '$disconnect').mockResolvedValue();
|
|
await database.onModuleDestroy();
|
|
expect(disconnect).toHaveBeenCalledTimes(1);
|
|
});
|
|
it('propagates connection failures', async () => {
|
|
jest
|
|
.spyOn(database, '$connect')
|
|
.mockRejectedValue(new Error('unavailable'));
|
|
await expect(database.onModuleInit()).rejects.toThrow('unavailable');
|
|
});
|
|
it('executes a database probe and propagates query failure', async () => {
|
|
const query = jest
|
|
.spyOn(database, '$queryRaw')
|
|
.mockResolvedValue([{ value: 1 }]);
|
|
await database.ping();
|
|
expect(query).toHaveBeenCalledTimes(1);
|
|
query.mockRejectedValue(new Error('timeout'));
|
|
await expect(database.ping()).rejects.toThrow('timeout');
|
|
});
|
|
});
|