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 a2e0b01 commit 6a6e01c
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,25 +723,32 @@ async function switchToNotificationWindow(driver) {
* mockServer method, this method will allow getting all of the seen requests
* for each mock in the array.
*
* @param {WebDriver} driver
* @param {import('mockttp').MockedEndpoint[]} mockedEndpoints
* @param {boolean} hasRequest
* @param {WebDriver} driver - The WebDriver instance.
* @param {import('mockttp').MockedEndpoint[]} mockedEndpoints - mockttp mocked endpoints
* @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 6a6e01c

Please sign in to comment.