Skip to content

Commit

Permalink
chore(ct): throw error when props are not json serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
sand4rt committed Mar 28, 2023
1 parent bfea952 commit 57175cb
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/playwright-test/src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export const fixtures: Fixtures<

mount: async ({ page }, use) => {
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 () => {
return await innerMount(page, component, options);
}, true);
Expand All @@ -72,6 +75,24 @@ export const fixtures: Fixtures<
},
};

const jsonValue: Record<string, Function> = {
string: (props: any) => typeof props === 'string',
number: (props: any) => typeof props === 'number',
boolean: (props: any) => typeof props === 'boolean',
null: (props: any) => props === null,
array: (props: any) => Array.isArray(props) && props.every(isJson),
object: (props: any) => typeof props === 'object' && props !== null && !Array.isArray(props)
&& Object.values(props).every(prop => isJson(prop)),
}

function isJson(value: any): boolean {
const valueType = Array.isArray(value) ? 'array' : typeof value;
const validate = jsonValue[valueType];
if (validate)
return validate(value);
return false;
}

function isJsxApi(options: Record<string, unknown>): options is JsxComponent {
return options?.kind === 'jsx';
}
Expand Down

0 comments on commit 57175cb

Please sign in to comment.