diff --git a/test/test.ts b/test/test.ts index d91da93..69748f8 100644 --- a/test/test.ts +++ b/test/test.ts @@ -10,6 +10,7 @@ import { ImportMock } from "ts-mock-imports"; import * as rateLimitMiddlewareModule from "../src/middleware/requestRateLimit"; import rateLimit from "express-rate-limit"; import redis from "../src/utils/redis"; +import { resetRedis, resetPostgres } from "./utils/reset"; async function init() { ImportMock.mockFunction(rateLimitMiddlewareModule, "rateLimitMiddleware", rateLimit({ @@ -19,6 +20,8 @@ async function init() { // delete old test database if (fs.existsSync(config.db)) fs.unlinkSync(config.db); if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB); + await resetRedis(); + await resetPostgres(); await initDb(); @@ -59,6 +62,7 @@ async function init() { server.close(); redis.quit(); process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures + process.exit(); }); }); }); diff --git a/test/utils/reset.ts b/test/utils/reset.ts new file mode 100644 index 0000000..d6e4915 --- /dev/null +++ b/test/utils/reset.ts @@ -0,0 +1,22 @@ +// drop postgres tables +// reset redis cache +import { config } from "../../src/config"; +import { createClient } from "redis"; +import { Pool } from "pg"; +import { Logger } from "../../src/utils/logger"; + +export async function resetRedis() { + if (config.redis) { + const client = createClient(config.redis); + await client.connect(); + await client.flushAll(); + } +} +export async function resetPostgres() { + if (process.env.TEST_POSTGRES && config.postgres) { + const pool = new Pool({ ...config.postgres }); + await pool.query(`DROP DATABASE IF EXISTS "sponsorTimes"`); + await pool.query(`DROP DATABASE IF EXISTS "privateDB"`); + await pool.end().catch(err => Logger.error(`closing db (postgres): ${err}`)); + } +} \ No newline at end of file