From 6953e40c7551169c2a768a736e6b14fa494f2cab Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 12 Feb 2021 19:41:55 -0500 Subject: [PATCH] tests: Wait for `common.init()` to complete before returning --- src/tests/backend/common.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/tests/backend/common.js b/src/tests/backend/common.js index 3fe80de3b..93536bc6d 100644 --- a/src/tests/backend/common.js +++ b/src/tests/backend/common.js @@ -9,7 +9,7 @@ const supertest = require('supertest'); const webaccess = require('../../node/hooks/express/webaccess'); const backups = {}; -let inited = false; +let agentPromise = null; exports.apiKey = apiHandler.exportedForTestingOnly.apiKey; exports.agent = null; @@ -24,8 +24,9 @@ const logLevel = exports.logger.level; process.on('unhandledRejection', (reason, promise) => { throw reason; }); exports.init = async function () { - if (inited) return exports.agent; - inited = true; + if (agentPromise != null) return await agentPromise; + let agentResolve; + agentPromise = new Promise((resolve) => { agentResolve = resolve; }); if (!logLevel.isLessThanOrEqualTo(log4js.levels.DEBUG)) { exports.logger.warn('Disabling non-test logging for the duration of the test. ' + @@ -56,5 +57,6 @@ exports.init = async function () { await server.exit(); }); + agentResolve(exports.agent); return exports.agent; };