fix(ct): throw error if inline component is getting mounted

This commit is contained in:
Max Schmitt 2024-09-10 10:40:09 +02:00
parent 8995ace825
commit 7b040a075d
2 changed files with 21 additions and 0 deletions

View file

@ -66,6 +66,12 @@ export function transformObject(value: any, mapping: (v: any) => { result: any }
result.push(transformObject(item, mapping)); result.push(transformObject(item, mapping));
return result; return result;
} }
if (value?.__pw_type === 'jsx' && typeof value.type === 'function') {
throw new Error([
'JSX component was not able to get resolved.',
'Make sure to define components outside of your test file.'
].join('\n'));
}
const result2: any = {}; const result2: any = {};
for (const [key, prop] of Object.entries(value)) for (const [key, prop] of Object.entries(value))
result2[key] = transformObject(prop, mapping); result2[key] = transformObject(prop, mapping);

View file

@ -2,6 +2,7 @@ import { test, expect } from '@playwright/experimental-ct-react';
import Button from '@/components/Button'; import Button from '@/components/Button';
import EmptyFragment from '@/components/EmptyFragment'; import EmptyFragment from '@/components/EmptyFragment';
import { ComponentAsProp } from '@/components/ComponentAsProp'; import { ComponentAsProp } from '@/components/ComponentAsProp';
import DefaultChildren from '@/components/DefaultChildren';
test('render props', async ({ mount }) => { test('render props', async ({ mount }) => {
const component = await mount(<Button title="Submit" />); const component = await mount(<Button title="Submit" />);
@ -31,3 +32,17 @@ test('render an empty component', async ({ mount, page }) => {
expect(await component.textContent()).toBe(''); expect(await component.textContent()).toBe('');
await expect(component).toHaveText(''); await expect(component).toHaveText('');
}); });
function MyInlineComponent({ value }: { value: string }) {
return <>Hello {value}</>;
}
test('render inline component with an error', async ({ mount }) => {
await expect(mount(<MyInlineComponent value="Max" />)).rejects.toThrow('JSX component was not able to get resolved');
});
test('render inline component with an error if its nested', async ({ mount }) => {
await expect(mount(<DefaultChildren>
<MyInlineComponent value="Max" />
</DefaultChildren>)).rejects.toThrow('JSX component was not able to get resolved');
});