From 3a4381303ccafdeb599f461d60bd55fa6425f57c Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 29 Feb 2024 19:13:32 -0800 Subject: [PATCH] cherry-pick(#29744): fix(ct): stop-gap for shared file import --- .../playwright-ct-core/src/tsxTransform.ts | 29 ++++++++++++++-- .../playwright.ct-react.spec.ts | 34 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/playwright-ct-core/src/tsxTransform.ts b/packages/playwright-ct-core/src/tsxTransform.ts index 0a61d4e3d9863..ab33c114ba66c 100644 --- a/packages/playwright-ct-core/src/tsxTransform.ts +++ b/packages/playwright-ct-core/src/tsxTransform.ts @@ -75,7 +75,7 @@ export default declare((api: BabelAPI) => { const ext = path.extname(importNode.source.value); // Convert all non-JS imports into refs. - if (!allJsExtensions.has(ext)) { + if (artifactExtensions.has(ext)) { for (const specifier of importNode.specifiers) { if (t.isImportNamespaceSpecifier(specifier)) continue; @@ -171,4 +171,29 @@ export function importInfo(importNode: T.ImportDeclaration, specifier: T.ImportS return { localName: specifier.local.name, info: result }; } -const allJsExtensions = new Set(['.js', '.jsx', '.cjs', '.mjs', '.ts', '.tsx', '.cts', '.mts', '']); +const artifactExtensions = new Set([ + // Frameworks + '.vue', + '.svelte', + + // Images + '.jpg', '.jpeg', + '.png', + '.gif', + '.svg', + '.bmp', + '.webp', + '.ico', + + // CSS + '.css', + + // Fonts + '.woff', '.woff2', + '.ttf', + '.otf', + '.eot', + + // Other assets + '.json', +]); \ No newline at end of file diff --git a/tests/playwright-test/playwright.ct-react.spec.ts b/tests/playwright-test/playwright.ct-react.spec.ts index a92f8297f19a1..6f1780ce55dfa 100644 --- a/tests/playwright-test/playwright.ct-react.spec.ts +++ b/tests/playwright-test/playwright.ct-react.spec.ts @@ -511,3 +511,37 @@ test('should allow props children', async ({ runInlineTest }) => { expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); }); + +test('should allow import from shared file', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': playwrightCtConfigText, + 'playwright/index.html': ``, + 'playwright/index.ts': ``, + 'src/component.tsx': ` + export const Component = (props: { content: string }) => { + return
{props.content}
+ }; + `, + 'src/component.shared.tsx': ` + export const componentMock = { content: 'This is a content.' }; + `, + 'src/component.render.tsx': ` + import {Component} from './component'; + import {componentMock} from './component.shared'; + export const ComponentTest = () => { + return ; + }; + `, + 'src/component.spec.tsx': ` + import { expect, test } from '@playwright/experimental-ct-react'; + import { ComponentTest } from './component.render'; + import { componentMock } from './component.shared'; + test('component renders', async ({ mount }) => { + const component = await mount(); + await expect(component).toContainText(componentMock.content) + })` + }, { workers: 1 }); + + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); +});