mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore(ct): throw error when props are not json serializable (#22025)
This commit is contained in:
parent
8b7dc2cf7a
commit
9c0c5d6e2a
@ -51,6 +51,8 @@ export const fixtures: Fixtures<
|
|||||||
|
|
||||||
mount: async ({ page }, use) => {
|
mount: async ({ page }, use) => {
|
||||||
await use(async (component: JsxComponent | string, options?: MountOptions) => {
|
await use(async (component: JsxComponent | string, options?: MountOptions) => {
|
||||||
|
if (options?.props && !isJson(options.props))
|
||||||
|
throw new Error('The mount function props are not JSON serializable.');
|
||||||
const selector = await (page as any)._wrapApiCall(async () => {
|
const selector = await (page as any)._wrapApiCall(async () => {
|
||||||
return await innerMount(page, component, options);
|
return await innerMount(page, component, options);
|
||||||
}, true);
|
}, true);
|
||||||
@ -63,7 +65,10 @@ export const fixtures: Fixtures<
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
update: async (options: JsxComponent | Omit<MountOptions, 'hooksConfig'>) => {
|
update: async (options: JsxComponent | Omit<MountOptions, 'hooksConfig'>) => {
|
||||||
if (isJsxApi(options)) return await innerUpdate(page, options);
|
if (isJsxApi(options))
|
||||||
|
return await innerUpdate(page, options);
|
||||||
|
if (options?.props && !isJson(options.props))
|
||||||
|
throw new Error('The update function props are not JSON serializable.');
|
||||||
await innerUpdate(page, component, options);
|
await innerUpdate(page, component, options);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -72,6 +77,24 @@ export const fixtures: Fixtures<
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const jsonType: Record<string, Function> = {
|
||||||
|
string: (value: any) => typeof value === 'string',
|
||||||
|
number: (value: any) => typeof value === 'number',
|
||||||
|
boolean: (value: any) => typeof value === 'boolean',
|
||||||
|
null: (value: any) => value === null,
|
||||||
|
array: (value: any) => Array.isArray(value) && value.every(isJson),
|
||||||
|
object: (value: any) => typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||||
|
&& Object.values(value).every(isJson),
|
||||||
|
};
|
||||||
|
|
||||||
|
function isJson(value: any): boolean {
|
||||||
|
const valueType = Array.isArray(value) ? 'array' : typeof value;
|
||||||
|
const validate = jsonType[valueType];
|
||||||
|
if (validate)
|
||||||
|
return validate(value);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
function isJsxApi(options: Record<string, unknown>): options is JsxComponent {
|
function isJsxApi(options: Record<string, unknown>): options is JsxComponent {
|
||||||
return options?.kind === 'jsx';
|
return options?.kind === 'jsx';
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user