fix(evaluate): fallback to toJSON if it exists when serializing (#15188)

* fix(evaluate): fallback to toJSON if it exists when serializing

* fix test in ff

* window.performance test
This commit is contained in:
Max Schmitt 2022-06-29 08:07:32 +02:00 committed by GitHub
parent 8c7f8eda9f
commit e34fa4feeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View File

@ -42,7 +42,11 @@ export function source() {
} }
function isError(obj: any): obj is Error { function isError(obj: any): obj is Error {
return obj instanceof Error || (obj && obj.__proto__ && obj.__proto__.name === 'Error'); try {
return obj instanceof Error || (obj && obj.__proto__ && obj.__proto__.name === 'Error');
} catch (error) {
return false;
}
} }
function parseEvaluationResultValue(value: SerializedValue, handles: any[] = [], refs: Map<number, object> = new Map()): any { function parseEvaluationResultValue(value: SerializedValue, handles: any[] = [], refs: Map<number, object> = new Map()): any {
@ -177,6 +181,11 @@ export function source() {
else else
o.push({ k: name, v: serialize(item, handleSerializer, visitorInfo) }); o.push({ k: name, v: serialize(item, handleSerializer, visitorInfo) });
} }
// If Object.keys().length === 0 we fall back to toJSON if it exists
if (o.length === 0 && value.toJSON && typeof value.toJSON === 'function')
return innerSerialize(value.toJSON(), handleSerializer, visitorInfo);
return { o, id }; return { o, id };
} }
} }

View File

@ -330,6 +330,53 @@ it('should properly serialize null fields', async ({ page }) => {
expect(await page.evaluate(() => ({ a: null }))).toEqual({ a: null }); expect(await page.evaluate(() => ({ a: null }))).toEqual({ a: null });
}); });
it('should properly serialize PerformanceMeasure object', async ({ page }) => {
expect(await page.evaluate(() => {
window.performance.mark('start');
window.performance.mark('end');
window.performance.measure('my-measure', 'start', 'end');
return performance.getEntriesByType('measure');
})).toEqual([{
duration: expect.any(Number),
entryType: 'measure',
name: 'my-measure',
startTime: expect.any(Number),
}]);
});
it('shuld properly serialize window.performance object', async ({ page }) => {
expect(await page.evaluate(() => performance)).toEqual({
'navigation': {
'redirectCount': 0,
'type': 0
},
'timeOrigin': expect.any(Number),
'timing': {
'connectEnd': expect.any(Number),
'connectStart': expect.any(Number),
'domComplete': expect.any(Number),
'domContentLoadedEventEnd': expect.any(Number),
'domContentLoadedEventStart': expect.any(Number),
'domInteractive': expect.any(Number),
'domLoading': expect.any(Number),
'domainLookupEnd': expect.any(Number),
'domainLookupStart': expect.any(Number),
'fetchStart': expect.any(Number),
'loadEventEnd': expect.any(Number),
'loadEventStart': expect.any(Number),
'navigationStart': expect.any(Number),
'redirectEnd': expect.any(Number),
'redirectStart': expect.any(Number),
'requestStart': expect.any(Number),
'responseEnd': expect.any(Number),
'responseStart': expect.any(Number),
'secureConnectionStart': expect.any(Number),
'unloadEventEnd': expect.any(Number),
'unloadEventStart': expect.any(Number),
}
});
});
it('should return undefined for non-serializable objects', async ({ page }) => { it('should return undefined for non-serializable objects', async ({ page }) => {
expect(await page.evaluate(() => function() {})).toBe(undefined); expect(await page.evaluate(() => function() {})).toBe(undefined);
}); });