fix(tests): look for json in thrown error (#8601)

This commit is contained in:
anthony bushong
2025-09-16 20:50:12 -07:00
committed by GitHub
parent 6391c4c0f1
commit f849c85608

View File

@@ -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);