Skip to content

Commit

Permalink
test: Fix getEventPayloads e2e test helper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Gudahtt committed Nov 28, 2024
1 parent 9b067c8 commit 21cd698
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<import('mockttp/dist/pluggable-admin').MockttpClientResponse[]>}
*/
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()));
Expand Down

0 comments on commit 21cd698

Please sign in to comment.