| 16 | import { Pool } from 'pg' |
| 17 | |
| 18 | export class TestContext { |
| 19 | #app?: App |
| 20 | |
| 21 | request = axios.create({ |
| 22 | baseURL: `http://localhost:${testConfig.port}`, |
| 23 | validateStatus: () => true, |
| 24 | }) |
| 25 | |
| 26 | get db(): Kysely<Database> { |
| 27 | return this.#app!.db |
| 28 | } |
| 29 | |
| 30 | before = async (): Promise<void> => { |
| 31 | const adminDb = new Kysely<any>({ |
| 32 | dialect: new PostgresDialect({ |
| 33 | pool: new Pool(testConfig.adminDatabase), |
| 34 | }), |
| 35 | }) |
| 36 | |
| 37 | // Create our test database |
| 38 | const { database } = testConfig.database |
| 39 | await sql`drop database if exists ${sql.id(database!)}`.execute(adminDb) |
| 40 | await sql`create database ${sql.id(database!)}`.execute(adminDb) |
| 41 | await adminDb.destroy() |
| 42 | |
| 43 | // Now connect to the test database and run the migrations |
| 44 | const db = new Kysely<any>({ |
| 45 | dialect: new PostgresDialect({ |
| 46 | pool: new Pool(testConfig.database), |
| 47 | }), |
| 48 | }) |
| 49 | |
| 50 | const migrator = new Migrator({ |
| 51 | db, |
| 52 | provider: new FileMigrationProvider({ |
| 53 | fs, |
| 54 | path, |
| 55 | migrationFolder: path.join(__dirname, '../src/migrations'), |
| 56 | }), |
| 57 | }) |
| 58 | |
| 59 | await migrator.migrateToLatest() |
| 60 | await db.destroy() |
| 61 | } |
| 62 | |
| 63 | after = async (): Promise<void> => { |
| 64 | // Nothing to do here at the moment |
| 65 | } |
| 66 | |
| 67 | beforeEach = async (): Promise<void> => { |
| 68 | this.#app = new App(testConfig) |
| 69 | |
| 70 | // Clear the database |
| 71 | await this.db.deleteFrom('user').execute() |
| 72 | |
| 73 | await this.#app.start() |
| 74 | } |
| 75 |
nothing calls this directly
no test coverage detected
searching dependent graphs…