diff --git a/packages/playwright-test/src/experimentalLoader.ts b/packages/playwright-test/src/experimentalLoader.ts index f10daec192bf6..3f7244a33525c 100644 --- a/packages/playwright-test/src/experimentalLoader.ts +++ b/packages/playwright-test/src/experimentalLoader.ts @@ -29,8 +29,9 @@ async function resolve(specifier: string, context: { parentURL?: string }, defau specifier = url.pathToFileURL(resolved).toString(); } const result = await defaultResolve(specifier, context, defaultResolve); - if (result?.url) + if (result?.url && result.url.startsWith('file://')) currentFileDepsCollector()?.add(url.fileURLToPath(result.url)); + return result; } diff --git a/tests/playwright-test/loader.spec.ts b/tests/playwright-test/loader.spec.ts index 08d359e4adbb7..883d81d14c05d 100644 --- a/tests/playwright-test/loader.spec.ts +++ b/tests/playwright-test/loader.spec.ts @@ -632,3 +632,32 @@ test('should import export assignment from ts', async ({ runInlineTest }) => { expect(result.passed).toBe(1); expect(result.exitCode).toBe(0); }); + +test('should support node imports', async ({ runInlineTest, nodeVersion }) => { + // We only support experimental esm mode on Node 16+ + test.skip(nodeVersion.major < 16); + const result = await runInlineTest({ + 'playwright.config.ts': 'export default {}', + 'package.json': JSON.stringify({ + type: 'module' + }), + 'test.json': 'test data', + 'utils.mjs': ` + import fs from "node:fs/promises"; + + export async function utilityModuleThatImportsNodeModule() { + return await fs.readFile('test.json', 'utf8'); + } + `, + 'a.test.ts': ` + import { test, expect } from '@playwright/test'; + import { utilityModuleThatImportsNodeModule } from './utils.mjs'; + + test('pass', async () => { + expect(await utilityModuleThatImportsNodeModule()).toBe('test data'); + }); + ` + }); + expect(result.passed).toBe(1); + expect(result.exitCode).toBe(0); +});