Skip to content

Commit

Permalink
chore: always grow component bundle (#20799)
Browse files Browse the repository at this point in the history
Fixes #20581
  • Loading branch information
pavelfeldman authored Feb 10, 2023
1 parent 5627618 commit 4469e57
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
18 changes: 7 additions & 11 deletions packages/playwright-test/src/plugins/vitePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export function createPlugin(
const hasNewComponents = await checkNewComponents(buildInfo, componentRegistry);
// 3. Check component sources.
const sourcesDirty = !buildExists || hasNewComponents || await checkSources(buildInfo);
// 4. Update component info.
buildInfo.components = [...componentRegistry.values()];

viteConfig.root = rootDir;
viteConfig.preview = { port, ...viteConfig.preview };
Expand Down Expand Up @@ -218,12 +220,6 @@ async function checkNewTests(suite: Suite, buildInfo: BuildInfo, componentRegist
componentRegistry.set(component.fullName, component);
buildInfo.tests[testFile] = { timestamp, components: components.map(c => c.fullName) };
hasNewTests = true;
} else {
// The test has not changed, populate component registry from the buildInfo.
for (const componentName of buildInfo.tests[testFile].components) {
const component = buildInfo.components.find(c => c.fullName === componentName)!;
componentRegistry.set(component.fullName, component);
}
}
}

Expand All @@ -232,7 +228,7 @@ async function checkNewTests(suite: Suite, buildInfo: BuildInfo, componentRegist

async function checkNewComponents(buildInfo: BuildInfo, componentRegistry: ComponentRegistry): Promise<boolean> {
const newComponents = [...componentRegistry.keys()];
const oldComponents = new Set(buildInfo.components.map(c => c.fullName));
const oldComponents = new Map(buildInfo.components.map(c => [c.fullName, c]));

let hasNewComponents = false;
for (const c of newComponents) {
Expand All @@ -241,10 +237,10 @@ async function checkNewComponents(buildInfo: BuildInfo, componentRegistry: Compo
break;
}
}
if (!hasNewComponents)
return false;
buildInfo.components = newComponents.map(n => componentRegistry.get(n)!);
return true;
for (const c of oldComponents.values())
componentRegistry.set(c.fullName, c);

return hasNewComponents;
}

async function parseTestFile(testFile: string): Promise<ComponentInfo[]> {
Expand Down
61 changes: 61 additions & 0 deletions tests/playwright-test/playwright.ct-build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,67 @@ test('should cache build', async ({ runInlineTest }, testInfo) => {
});
});

test('should grow cache', async ({ runInlineTest }, testInfo) => {
test.slow();

await test.step('original test', async () => {
const result = await runInlineTest({
'playwright.config.ts': playwrightConfig,
'playwright/index.html': `<script type="module" src="./index.ts"></script>`,
'playwright/index.ts': ``,
'src/button1.tsx': `
export const Button1 = () => <button>Button 1</button>;
`,
'src/button2.tsx': `
export const Button2 = () => <button>Button 2</button>;
`,
'src/button1.test.tsx': `
//@no-header
import { test, expect } from '@playwright/experimental-ct-react';
import { Button1 } from './button1.tsx';
test('pass', async ({ mount }) => {
const component = await mount(<Button1></Button1>);
await expect(component).toHaveText('Button 1');
});
`,
'src/button2.test.tsx': `
//@no-header
import { test, expect } from '@playwright/experimental-ct-react';
import { Button2 } from './button2.tsx';
test('pass', async ({ mount }) => {
const component = await mount(<Button2></Button2>);
await expect(component).toHaveText('Button 2');
});
`,
}, { workers: 1 }, undefined, { additionalArgs: ['button1'] });

expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
const output = result.output;
expect(output).toContain('modules transformed');
});

await test.step('run second test', async () => {
const result = await runInlineTest({
'playwright.config.ts': playwrightConfig,
}, { workers: 1 }, undefined, { additionalArgs: ['button2'] });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
const output = result.output;
expect(output).toContain('modules transformed');
});

await test.step('run first test again', async () => {
const result = await runInlineTest({
'playwright.config.ts': playwrightConfig,
}, { workers: 1 }, undefined, { additionalArgs: ['button2'] });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
const output = result.output;
expect(output).not.toContain('modules transformed');
});
});

test('should not use global config for preview', async ({ runInlineTest }) => {
const result1 = await runInlineTest({
'playwright.config.ts': playwrightConfig,
Expand Down

0 comments on commit 4469e57

Please sign in to comment.