From 15bf95f407da9c6f7db8a2589c8ec6a8608bd8b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ahlert?= Date: Thu, 30 Apr 2026 11:15:00 -0300 Subject: [PATCH] fix: anchor fetchThrowsOn regexes to avoid false positives on 2xx/3xx The default `fetchThrowsOn` patterns `/4.*/` and `/5.*/` are unanchored, so any status code containing the digit 4 or 5 matches and throws. This incorrectly fails 204, 304, 250-259, etc. Anchor to `/^4/` and `/^5/` so only 4xx and 5xx throw, matching the documented behavior ("fetch throws on non-2xx responses"). Fixes #670 --- src/core/config.js | 2 +- test/commands/fetch.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/config.js b/src/core/config.js index 7c3c11299..ab596a47b 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -4,7 +4,7 @@ export const config = { attributes: "_, script, data-script", defaultTransition: "all 500ms ease-in", disableSelector: "[disable-scripting], [data-disable-scripting]", - fetchThrowsOn: [/4.*/, /5.*/], + fetchThrowsOn: [/^4/, /^5/], hideShowStrategies: {}, logAll: false, mutatingMethods: { diff --git a/test/commands/fetch.js b/test/commands/fetch.js index 965ada56d..c1a5d47d2 100644 --- a/test/commands/fetch.js +++ b/test/commands/fetch.js @@ -196,6 +196,24 @@ test.describe("the fetch command", () => { await expect(find('div')).toHaveText("the body"); }); + test("does not throw on 204 No Content", async ({html, find, page}) => { + await page.route('**/test', async (route) => { + await route.fulfill({ status: 204, body: '' }); + }); + await html("
"); + await find('div').dispatchEvent('click'); + await expect(find('div')).toHaveText("ok"); + }); + + test("does not throw on 304 Not Modified", async ({html, find, page}) => { + await page.route('**/test', async (route) => { + await route.fulfill({ status: 304, body: '' }); + }); + await html("
"); + await find('div').dispatchEvent('click'); + await expect(find('div')).toHaveText("ok"); + }); + test("as response does not throw on 404", async ({html, find, page}) => { await page.route('**/test', async (route) => { await route.fulfill({ status: 404, body: 'not found' });