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"); const mysql = require("mysql2/promise");
let globalDb;
async function connectToDb() { async function connectToDb() {
const db = await mysql.createConnection({ if (globalDb) {
return globalDb;
}
globalDb = await mysql.createPool({
connectionLimit: 5,
host: "impress.openneo.net", host: "impress.openneo.net",
user: process.env["IMPRESS_MYSQL_USER"], user: process.env["IMPRESS_MYSQL_USER"],
password: process.env["IMPRESS_MYSQL_PASSWORD"], password: process.env["IMPRESS_MYSQL_PASSWORD"],
database: "openneo_impress", database: "openneo_impress",
}); });
return db; return globalDb;
} }
module.exports = connectToDb; 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! // keep an eye on perf - watch for tests with way too many queries!
jest.mock("./db"); jest.mock("./db");
let queryFn; let queryFn;
let db;
beforeEach(() => { beforeEach(() => {
connectToDb.mockImplementation(async (...args) => { connectToDb.mockImplementation(async (...args) => {
const db = await actualConnectToDb(...args); db = await actualConnectToDb(...args);
queryFn = jest.spyOn(db, "execute"); queryFn = jest.spyOn(db, "execute");
return db; return db;
}); });
}); });
afterEach(() => { afterEach(() => {
jest.resetAllMocks(); jest.resetAllMocks();
db.end();
db = null;
}); });
it("can load items", async () => { it("can load items", async () => {