From 5074b17abab516b892178715486bb7b8f039925d Mon Sep 17 00:00:00 2001 From: Matt Dunn-Rankin Date: Wed, 22 Apr 2020 15:53:59 -0700 Subject: [PATCH] fix db connection closing in tests --- src/server/db.js | 11 +++++++++-- src/server/index.test.js | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/server/db.js b/src/server/db.js index 785ede2..9b3b737 100644 --- a/src/server/db.js +++ b/src/server/db.js @@ -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; diff --git a/src/server/index.test.js b/src/server/index.test.js index 269235c..ee1abf3 100644 --- a/src/server/index.test.js +++ b/src/server/index.test.js @@ -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 () => {