Skip to content

Commit c85440b

Browse files
committed
fix(ct): stop-gap for shared file import
1 parent d48aada commit c85440b

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

packages/playwright-ct-core/src/tsxTransform.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,34 @@ export default declare((api: BabelAPI) => {
7474

7575
const ext = path.extname(importNode.source.value);
7676

77-
// Convert all non-JS imports into refs.
78-
if (!allJsExtensions.has(ext)) {
77+
if (jsxComponentNames.size) {
78+
let importCount = 0;
79+
// Convert JS imports that are used as components in JSX expressions into refs.
7980
for (const specifier of importNode.specifiers) {
8081
if (t.isImportNamespaceSpecifier(specifier))
8182
continue;
8283
const { localName, info } = importInfo(importNode, specifier, this.filename!);
83-
importInfos.set(localName, info);
84+
if (jsxComponentNames.has(localName)) {
85+
importInfos.set(localName, info);
86+
++importCount;
87+
}
88+
}
89+
// All the imports were from JSX => delete.
90+
if (importCount && importCount === importNode.specifiers.length) {
91+
p.skip();
92+
p.remove();
8493
}
85-
p.skip();
86-
p.remove();
8794
return;
8895
}
8996

90-
// Convert JS imports that are used as components in JSX expressions into refs.
91-
let importCount = 0;
92-
for (const specifier of importNode.specifiers) {
93-
if (t.isImportNamespaceSpecifier(specifier))
94-
continue;
95-
const { localName, info } = importInfo(importNode, specifier, this.filename!);
96-
if (jsxComponentNames.has(localName)) {
97+
// Convert all non-JS imports into refs.
98+
if (!allJsExtensions.has(ext)) {
99+
for (const specifier of importNode.specifiers) {
100+
if (t.isImportNamespaceSpecifier(specifier))
101+
continue;
102+
const { localName, info } = importInfo(importNode, specifier, this.filename!);
97103
importInfos.set(localName, info);
98-
++importCount;
99104
}
100-
}
101-
102-
// All the imports were from JSX => delete.
103-
if (importCount && importCount === importNode.specifiers.length) {
104105
p.skip();
105106
p.remove();
106107
}

tests/playwright-test/playwright.ct-react.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,37 @@ test('should allow props children', async ({ runInlineTest }) => {
511511
expect(result.exitCode).toBe(0);
512512
expect(result.passed).toBe(1);
513513
});
514+
515+
test('should allow import from shared file', async ({ runInlineTest }) => {
516+
const result = await runInlineTest({
517+
'playwright.config.ts': playwrightCtConfigText,
518+
'playwright/index.html': `<script type="module" src="./index.ts"></script>`,
519+
'playwright/index.ts': ``,
520+
'src/component.tsx': `
521+
export const Component = (props: { content: string }) => {
522+
return <div>{props.content}</div>
523+
};
524+
`,
525+
'src/component.shared.tsx': `
526+
export const componentMock = { content: 'This is a content.' };
527+
`,
528+
'src/component.render.tsx': `
529+
import {Component} from './component';
530+
import {componentMock} from './component.shared';
531+
export const ComponentTest = () => {
532+
return <Component content={componentMock.content} />;
533+
};
534+
`,
535+
'src/component.spec.tsx': `
536+
import { expect, test } from '@playwright/experimental-ct-react';
537+
import { ComponentTest } from './component.render';
538+
import { componentMock } from './component.shared';
539+
test('component renders', async ({ mount }) => {
540+
const component = await mount(<ComponentTest />);
541+
await expect(component).toContainText(componentMock.content)
542+
})`
543+
}, { workers: 1 });
544+
545+
expect(result.exitCode).toBe(0);
546+
expect(result.passed).toBe(1);
547+
});

0 commit comments

Comments
 (0)