swaf/test/_app.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

import config from "config";
import MysqlConnectionManager from "../src/db/MysqlConnectionManager.js";
import TestApp from "../src/TestApp.js";
import {setupMailServer, teardownMailServer} from "./_mail_server.js";
export default function useApp<T extends TestApp>(appSupplier: AppSupplier<T>): () => T {
let app: T;
2021-11-08 00:52:33 +01:00
beforeAll(async () => {
await MysqlConnectionManager.prepare();
await MysqlConnectionManager.query('DROP DATABASE IF EXISTS ' + config.get<string>('mysql.database'));
await MysqlConnectionManager.endPool();
await setupMailServer();
app = await appSupplier('127.0.0.1', 8966);
await app.start();
});
2021-11-08 00:52:33 +01:00
afterAll(async () => {
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;
});
return () => app;
}
export type AppSupplier<T extends TestApp> = (addr: string, port: number) => Promise<T>;