Skip to content

Commit 05bd050

Browse files
committed
Add and use withRecordingLoggerAsync
1 parent 325a3a2 commit 05bd050

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/start-proxy.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
getRecordingLogger,
1616
makeTestToken,
1717
setupTests,
18+
withRecordingLoggerAsync,
1819
} from "./testing-utils";
1920

2021
setupTests(test);
@@ -390,29 +391,29 @@ const wrapFailureTest = test.macro({
390391
setup: () => void,
391392
fn: (logger: Logger) => Promise<void>,
392393
) => {
393-
const loggedMessages = [];
394-
const logger = getRecordingLogger(loggedMessages);
395-
setup();
394+
await withRecordingLoggerAsync(async (logger) => {
395+
setup();
396396

397-
await t.throwsAsync(fn(logger), {
398-
instanceOf: startProxyExports.StartProxyError,
397+
await t.throwsAsync(fn(logger), {
398+
instanceOf: startProxyExports.StartProxyError,
399+
});
399400
});
400401
},
401402
title: (providedTitle) => `${providedTitle} - wraps errors on failure`,
402403
});
403404

404405
test("downloadProxy - returns file path on success", async (t) => {
405-
const loggedMessages = [];
406-
const logger = getRecordingLogger(loggedMessages);
407-
const testPath = "/some/path";
408-
sinon.stub(toolcache, "downloadTool").resolves(testPath);
409-
410-
const result = await startProxyExports.downloadProxy(
411-
logger,
412-
"url",
413-
undefined,
414-
);
415-
t.is(result, testPath);
406+
await withRecordingLoggerAsync(async (logger) => {
407+
const testPath = "/some/path";
408+
sinon.stub(toolcache, "downloadTool").resolves(testPath);
409+
410+
const result = await startProxyExports.downloadProxy(
411+
logger,
412+
"url",
413+
undefined,
414+
);
415+
t.is(result, testPath);
416+
});
416417
});
417418

418419
test(

src/testing-utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ export function checkExpectedLogMessages(
208208
}
209209
}
210210

211+
/**
212+
* Initialises a recording logger and calls `body` with it.
213+
*
214+
* @param body The test that requires a recording logger.
215+
* @returns The logged messages.
216+
*/
217+
export async function withRecordingLoggerAsync(
218+
body: (logger: Logger) => Promise<void>,
219+
): Promise<LoggedMessage[]> {
220+
const messages = [];
221+
const logger = getRecordingLogger(messages);
222+
223+
await body(logger);
224+
225+
return messages;
226+
}
227+
211228
/** Mock the HTTP request to the feature flags enablement API endpoint. */
212229
export function mockFeatureFlagApiEndpoint(
213230
responseStatusCode: number,

0 commit comments

Comments
 (0)