tests: Wait for `common.init()` to complete before returning

pull/4770/head
Richard Hansen 2021-02-12 19:41:55 -05:00 committed by John McLear
parent 1c8a913411
commit 6953e40c75
1 changed files with 5 additions and 3 deletions

View File

@ -9,7 +9,7 @@ const supertest = require('supertest');
const webaccess = require('../../node/hooks/express/webaccess'); const webaccess = require('../../node/hooks/express/webaccess');
const backups = {}; const backups = {};
let inited = false; let agentPromise = null;
exports.apiKey = apiHandler.exportedForTestingOnly.apiKey; exports.apiKey = apiHandler.exportedForTestingOnly.apiKey;
exports.agent = null; exports.agent = null;
@ -24,8 +24,9 @@ const logLevel = exports.logger.level;
process.on('unhandledRejection', (reason, promise) => { throw reason; }); process.on('unhandledRejection', (reason, promise) => { throw reason; });
exports.init = async function () { exports.init = async function () {
if (inited) return exports.agent; if (agentPromise != null) return await agentPromise;
inited = true; let agentResolve;
agentPromise = new Promise((resolve) => { agentResolve = resolve; });
if (!logLevel.isLessThanOrEqualTo(log4js.levels.DEBUG)) { if (!logLevel.isLessThanOrEqualTo(log4js.levels.DEBUG)) {
exports.logger.warn('Disabling non-test logging for the duration of the test. ' + exports.logger.warn('Disabling non-test logging for the duration of the test. ' +
@ -56,5 +57,6 @@ exports.init = async function () {
await server.exit(); await server.exit();
}); });
agentResolve(exports.agent);
return exports.agent; return exports.agent;
}; };