fix db connection closing in tests

This commit is contained in:
Matt Dunn-Rankin 2020-04-22 15:53:59 -07:00
parent d1d98e2f72
commit 5074b17aba
2 changed files with 13 additions and 3 deletions

View file

@ -1,14 +1,21 @@
const mysql = require("mysql2/promise");
let globalDb;
async function connectToDb() {
const db = await mysql.createConnection({
if (globalDb) {
return globalDb;
}
globalDb = await mysql.createPool({
connectionLimit: 5,
host: "impress.openneo.net",
user: process.env["IMPRESS_MYSQL_USER"],
password: process.env["IMPRESS_MYSQL_PASSWORD"],
database: "openneo_impress",
});
return db;
return globalDb;
}
module.exports = connectToDb;

View file

@ -12,15 +12,18 @@ const { query } = createTestClient(new ApolloServer(config));
// keep an eye on perf - watch for tests with way too many queries!
jest.mock("./db");
let queryFn;
let db;
beforeEach(() => {
connectToDb.mockImplementation(async (...args) => {
const db = await actualConnectToDb(...args);
db = await actualConnectToDb(...args);
queryFn = jest.spyOn(db, "execute");
return db;
});
});
afterEach(() => {
jest.resetAllMocks();
db.end();
db = null;
});
it("can load items", async () => {