Why does the export test sometimes miss its network response?

Instruction: Correct the listener ordering and write useful response assertions without hiding an error status.

Context:

A fictional export page has one Create export button and one status region. Clicking makes exactly one POST /api/exports to the page origin for dataset orders. It can respond immediately. Background GETs use the same path. Success is HTTP 202 with JSON containing a nonempty string job_id and dataset: orders, followed by status text Export queued. No concurrent POSTs are made in this fixture. Existing code clicks first and then calls page.waitForResponse.

await page.getByRole('button', { name: 'Create export' }).click();
const response = await page.waitForResponse('**/api/exports');

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

The response can arrive before the listener exists. I would register the response wait before starting the click, and await both operations...

Related Questions