Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support import components from node_modules #30458

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/playwright-ct-core/src/viteUtils.ts
Expand Up @@ -183,8 +183,8 @@ export function transformIndexFile(id: string, content: string, templateDir: str
lines.push(registerSource);

for (const value of importInfos.values()) {
const importPath = resolveHook(value.filename, value.importSource);
lines.push(`const ${value.id} = () => import('${importPath?.replaceAll(path.sep, '/')}').then((mod) => mod.${value.remoteName || 'default'});`);
const importPath = resolveHook(value.filename, value.importSource) ?? value.importSource;
lines.push(`const ${value.id} = () => import('${importPath.replaceAll(path.sep, '/')}').then((mod) => mod.${value.remoteName || 'default'});`);
}

lines.push(`__pwRegistry.initialize({ ${[...importInfos.keys()].join(',\n ')} });`);
Expand Down
27 changes: 27 additions & 0 deletions tests/playwright-test/playwright.ct-react.spec.ts
Expand Up @@ -545,3 +545,30 @@ test('should allow import from shared file', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});

test('should allow import from node modules', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': playwrightCtConfigText,
'playwright/index.html': `<script type="module" src="./index.ts"></script>`,
'playwright/index.ts': ``,
'node_modules/test-module/index.js': `
export function Component() {
return "hello playwright";
};
`,
'node_modules/test-module/package.json': `{
"name": "test-module",
"main": "./index.js"
}`,
'src/component.spec.tsx': `
import { expect, test } from '@playwright/experimental-ct-react';
import { Component } from 'test-module';
test('component renders', async ({ mount }) => {
const component = await mount(<Component />);
await expect(component).toContainText("hello playwright")
})`
}, { workers: 1 });

expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});