From 05bd050f34dab49564f92e80d2efe16c7058a843 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Thu, 29 Jan 2026 13:44:10 +0000 Subject: [PATCH] Add and use `withRecordingLoggerAsync` --- src/start-proxy.test.ts | 31 ++++++++++++++++--------------- src/testing-utils.ts | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/start-proxy.test.ts b/src/start-proxy.test.ts index 3c1df5608..640a1da87 100644 --- a/src/start-proxy.test.ts +++ b/src/start-proxy.test.ts @@ -15,6 +15,7 @@ import { getRecordingLogger, makeTestToken, setupTests, + withRecordingLoggerAsync, } from "./testing-utils"; setupTests(test); @@ -390,29 +391,29 @@ const wrapFailureTest = test.macro({ setup: () => void, fn: (logger: Logger) => Promise, ) => { - const loggedMessages = []; - const logger = getRecordingLogger(loggedMessages); - setup(); + await withRecordingLoggerAsync(async (logger) => { + setup(); - await t.throwsAsync(fn(logger), { - instanceOf: startProxyExports.StartProxyError, + await t.throwsAsync(fn(logger), { + instanceOf: startProxyExports.StartProxyError, + }); }); }, title: (providedTitle) => `${providedTitle} - wraps errors on failure`, }); test("downloadProxy - returns file path on success", async (t) => { - const loggedMessages = []; - const logger = getRecordingLogger(loggedMessages); - const testPath = "/some/path"; - sinon.stub(toolcache, "downloadTool").resolves(testPath); + await withRecordingLoggerAsync(async (logger) => { + const testPath = "/some/path"; + sinon.stub(toolcache, "downloadTool").resolves(testPath); - const result = await startProxyExports.downloadProxy( - logger, - "url", - undefined, - ); - t.is(result, testPath); + const result = await startProxyExports.downloadProxy( + logger, + "url", + undefined, + ); + t.is(result, testPath); + }); }); test( diff --git a/src/testing-utils.ts b/src/testing-utils.ts index 83ec88ee1..aff778043 100644 --- a/src/testing-utils.ts +++ b/src/testing-utils.ts @@ -208,6 +208,23 @@ export function checkExpectedLogMessages( } } +/** + * Initialises a recording logger and calls `body` with it. + * + * @param body The test that requires a recording logger. + * @returns The logged messages. + */ +export async function withRecordingLoggerAsync( + body: (logger: Logger) => Promise, +): Promise { + const messages = []; + const logger = getRecordingLogger(messages); + + await body(logger); + + return messages; +} + /** Mock the HTTP request to the feature flags enablement API endpoint. */ export function mockFeatureFlagApiEndpoint( responseStatusCode: number,