From 8b18b1c9febfa632bf58c75f16e6be20e5c49561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Wed, 14 Oct 2026 18:59:33 +0200 Subject: [PATCH] test(e2e): fail clearly on a medications POST body that is not JSON The route stub parsed the body inside the un-awaited route callback, so a malformed body threw there and the request hung. The parse error is now recorded and the route is fulfilled; reading capture.postBody in the spec throws with the parse error. --- e2e/medications-wizard-helpers.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/e2e/medications-wizard-helpers.ts b/e2e/medications-wizard-helpers.ts index d411cea39..921fa101a 100644 --- a/e2e/medications-wizard-helpers.ts +++ b/e2e/medications-wizard-helpers.ts @@ -30,7 +30,20 @@ interface StubbedMedication { } export function mockMedicationsApi(page: Page, stubbed: StubbedMedication) { - const capture: { postBody: unknown } = { postBody: null }; + // A body that is not JSON must fail the spec where it reads `postBody`, + // with the parse error, not throw inside the un-awaited route callback. + const capture = { + rawPostBody: null as unknown, + parseError: null as string | null, + get postBody(): unknown { + if (this.parseError !== null) { + throw new Error( + `POST /api/medications sent a body that is not JSON: ${this.parseError}`, + ); + } + return this.rawPostBody; + }, + }; const listEntry = { id: stubbed.id, @@ -68,7 +81,11 @@ export function mockMedicationsApi(page: Page, stubbed: StubbedMedication) { return; } if (req.method() === "POST") { - capture.postBody = JSON.parse(req.postData() ?? "null"); + try { + capture.rawPostBody = JSON.parse(req.postData() ?? "null"); + } catch (err) { + capture.parseError = err instanceof Error ? err.message : String(err); + } await route.fulfill({ status: 201, contentType: "application/json",