From 783d8d70d8fb3c0e6c6e6901ff15d9cf58357c8a Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Mon, 22 Apr 2024 16:42:52 +0800 Subject: [PATCH 1/2] feat: add test case --- .../playwright.ct-react.spec.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/playwright-test/playwright.ct-react.spec.ts b/tests/playwright-test/playwright.ct-react.spec.ts index 6f1780ce55dfa..fad204b865410 100644 --- a/tests/playwright-test/playwright.ct-react.spec.ts +++ b/tests/playwright-test/playwright.ct-react.spec.ts @@ -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': ``, + '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(); + await expect(component).toContainText("hello playwright") + })` + }, { workers: 1 }); + + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); +}); From 2d1e742142e869fd07a6766353b58aa4861adf91 Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Mon, 22 Apr 2024 17:00:25 +0800 Subject: [PATCH 2/2] fix: import path --- packages/playwright-ct-core/src/viteUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/playwright-ct-core/src/viteUtils.ts b/packages/playwright-ct-core/src/viteUtils.ts index 505301c48e9ed..28cdded872494 100644 --- a/packages/playwright-ct-core/src/viteUtils.ts +++ b/packages/playwright-ct-core/src/viteUtils.ts @@ -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 ')} });`);