From f849c856084120f11b38dfdbdd97f7bb49b68c2a Mon Sep 17 00:00:00 2001 From: anthony bushong Date: Tue, 16 Sep 2025 20:50:12 -0700 Subject: [PATCH] fix(tests): look for json in thrown error (#8601) --- integration-tests/json-output.test.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/integration-tests/json-output.test.ts b/integration-tests/json-output.test.ts index a998d4a731..7a65a65133 100644 --- a/integration-tests/json-output.test.ts +++ b/integration-tests/json-output.test.ts @@ -55,9 +55,27 @@ describe('JSON output', () => { expect(thrown).toBeDefined(); const message = (thrown as Error).message; - const jsonStart = message.indexOf('{'); - expect(jsonStart).toBeGreaterThan(-1); - const payload = JSON.parse(message.slice(jsonStart)); + + // Use a regex to find the first complete JSON object in the string + const jsonMatch = message.match(/{[\s\S]*}/); + + // Fail if no JSON-like text was found + expect( + jsonMatch, + 'Expected to find a JSON object in the error output', + ).toBeTruthy(); + + let payload; + try { + // Parse the matched JSON string + payload = JSON.parse(jsonMatch![0]); + } catch (parseError) { + console.error('Failed to parse the following JSON:', jsonMatch![0]); + throw new Error( + `Test failed: Could not parse JSON from error message. Details: ${parseError}`, + ); + } + expect(payload.error).toBeDefined(); expect(payload.error.type).toBe('Error'); expect(payload.error.code).toBe(1);