From 21cd698f0c4660c3bb0388e7b6d682bd7e053727 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 28 Nov 2024 14:00:57 -0330 Subject: [PATCH] test: Fix `getEventPayloads` e2e test helper The test helper `getEventPayloads` is meant to wait until all mock endpoints provided have resolved (i.e. until none are pending). However, it currently only waits until the _last_ mock endpoint in the array has been fulfilled. The pending status of the others is ignored. This results in race conditions, because the status of non-last mock endpoints is indeterminate when this returns. The `hasRequest` parameter has been renamed to `waitWhilePending` to make the expected behavior more clear, and JSDocs have been added for each parameter. --- test/e2e/helpers.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 75eefb12f7c1..cd8b2f429ed7 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -725,23 +725,30 @@ async function switchToNotificationWindow(driver) { * * @param {WebDriver} driver * @param {import('mockttp').MockedEndpoint[]} mockedEndpoints - * @param {boolean} hasRequest + * @param {boolean} [waitWhilePending] - Wait until no requests are pending * @returns {Promise} */ -async function getEventPayloads(driver, mockedEndpoints, hasRequest = true) { - await driver.wait( - async () => { - let isPending = true; - - for (const mockedEndpoint of mockedEndpoints) { - isPending = await mockedEndpoint.isPending(); - } +async function getEventPayloads( + driver, + mockedEndpoints, + waitWhilePending = true, +) { + if (waitWhilePending) { + await driver.wait( + async () => { + const pendingStatuses = await Promise.all( + mockedEndpoints.map((mockedEndpoint) => mockedEndpoint.isPending()), + ); + const isSomethingPending = pendingStatuses.some( + (pendingStatus) => pendingStatus, + ); - return isPending === !hasRequest; - }, - driver.timeout, - true, - ); + return !isSomethingPending; + }, + driver.timeout, + true, + ); + } const mockedRequests = []; for (const mockedEndpoint of mockedEndpoints) { mockedRequests.push(...(await mockedEndpoint.getSeenRequests()));