2020-11-16 12:08:39 +01:00
|
|
|
import config from "config";
|
2020-11-11 18:59:38 +01:00
|
|
|
|
2021-05-03 19:29:22 +02:00
|
|
|
import MysqlConnectionManager from "../src/db/MysqlConnectionManager.js";
|
|
|
|
import TestApp from "../src/TestApp.js";
|
|
|
|
import {setupMailServer, teardownMailServer} from "./_mail_server.js";
|
|
|
|
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2021-04-22 15:38:24 +02:00
|
|
|
export default function useApp<T extends TestApp>(appSupplier: AppSupplier<T>): () => T {
|
|
|
|
let app: T;
|
2020-07-08 10:49:29 +02:00
|
|
|
|
2021-11-08 00:52:33 +01:00
|
|
|
beforeAll(async () => {
|
2020-11-16 12:08:39 +01:00
|
|
|
await MysqlConnectionManager.prepare();
|
|
|
|
await MysqlConnectionManager.query('DROP DATABASE IF EXISTS ' + config.get<string>('mysql.database'));
|
|
|
|
await MysqlConnectionManager.endPool();
|
|
|
|
|
2020-07-08 10:49:29 +02:00
|
|
|
await setupMailServer();
|
2021-04-22 15:38:24 +02:00
|
|
|
app = await appSupplier('127.0.0.1', 8966);
|
2020-07-08 10:49:29 +02:00
|
|
|
|
|
|
|
await app.start();
|
|
|
|
});
|
|
|
|
|
2021-11-08 00:52:33 +01:00
|
|
|
afterAll(async () => {
|
2020-09-25 23:42:15 +02:00
|
|
|
const errors = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await app.stop();
|
|
|
|
} catch (e) {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await teardownMailServer();
|
|
|
|
} catch (e) {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length > 0) throw errors;
|
2020-07-08 10:49:29 +02:00
|
|
|
});
|
2021-04-22 15:38:24 +02:00
|
|
|
|
|
|
|
return () => app;
|
2020-07-08 10:49:29 +02:00
|
|
|
}
|
2021-03-26 10:38:58 +01:00
|
|
|
|
2021-04-22 15:38:24 +02:00
|
|
|
export type AppSupplier<T extends TestApp> = (addr: string, port: number) => Promise<T>;
|