chore: improve error message when Array.toJSON misbehaves (#32508)

Fixes: #32507
This commit is contained in:
Rui Figueira 2024-09-10 12:14:24 +01:00 committed by GitHub
parent 9fa06be49e
commit f8562e4ca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View file

@ -846,6 +846,8 @@ export class PageBinding {
const handle = await context.evaluateHandle(takeHandle, { name, seq }).catch(e => null); const handle = await context.evaluateHandle(takeHandle, { name, seq }).catch(e => null);
result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, handle); result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, handle);
} else { } else {
if (!Array.isArray(serializedArgs))
throw new Error(`serializedArgs is not an array. This can happen when Array.prototype.toJSON is defined incorrectly`);
const args = serializedArgs!.map(a => parseEvaluationResultValue(a)); const args = serializedArgs!.map(a => parseEvaluationResultValue(a));
result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, ...args); result = await binding.playwrightFunction({ frame: context.frame, page, context: page._browserContext }, ...args);
} }

View file

@ -300,3 +300,12 @@ it('should work with busted Array.prototype.map/push', async ({ page, server })
await page.exposeFunction('add', (a, b) => a + b); await page.exposeFunction('add', (a, b) => a + b);
expect(await page.evaluate('add(5, 6)')).toBe(11); expect(await page.evaluate('add(5, 6)')).toBe(11);
}); });
it('should fail with busted Array.prototype.toJSON', async ({ page }) => {
await page.evaluateHandle(() => (Array.prototype as any).toJSON = () => '"[]"');
await page.exposeFunction('add', (a, b) => a + b);
await expect(() => page.evaluate(`add(5, 6)`)).rejects.toThrowError('serializedArgs is not an array. This can happen when Array.prototype.toJSON is defined incorrectly');
expect.soft(await page.evaluate(() => ([] as any).toJSON())).toBe('"[]"');
});