Created
August 7, 2024 22:31
-
-
Save paulodutra/026f932802f12af40fb441c50e445f53 to your computer and use it in GitHub Desktop.
Configuration of test module to run the migration and seed before run the test cases.
This file contains hidden or 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
import { Test, TestingModule } from '@nestjs/testing'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
import { Connection } from 'typeorm'; | |
import { TaskSeedService } from '../../infrastructure/database/seeds/task-seed-service'; | |
import { TaskService } from './task.service'; | |
import { Task } from '../../infrastructure/database/entities/task.entity'; | |
import { DatabaseModule } from '../../infrastructure/database/database.module'; | |
describe('TaskService', () => { | |
let service: TaskService; | |
let taskSeedService: TaskSeedService; | |
let connection: Connection; | |
beforeAll(async () => { | |
const module: TestingModule = await Test.createTestingModule({ | |
imports: [DatabaseModule.forRoot(), TypeOrmModule.forFeature([Task])], | |
providers: [TaskService], | |
}).compile(); | |
service = module.get<TaskService>(TaskService); | |
taskSeedService = module.get<TaskSeedService>(TaskSeedService); | |
connection = module.get<Connection>(Connection); | |
await connection.runMigrations(); | |
await taskSeedService.seed(); | |
}, 10000); | |
afterAll(async () => { | |
console.log('my connection', connection['@instanceof']); | |
if (connection.isConnected) { | |
let executedMigrations = await connection.query( | |
'SELECT * FROM migrations ORDER BY timestamp DESC', | |
); | |
while (executedMigrations.length > 0) { | |
await connection.undoLastMigration(); | |
executedMigrations = await connection.query( | |
'SELECT * FROM migrations ORDER BY timestamp DESC', | |
); | |
} | |
await connection.close(); | |
} | |
}, 10000); | |
it('should be defined', () => { | |
expect(service).toBeDefined(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment