From d7be1fcca827ef23e139c63864dc69129361bf22 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 23 Aug 2022 10:20:56 -0700 Subject: [PATCH] fix(esm): allow importing ts from esm (#16735) --- packages/playwright-test/src/cli.ts | 2 -- packages/playwright-test/src/transform.ts | 4 +--- tests/playwright-test/loader.spec.ts | 19 +++++++++++++------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/playwright-test/src/cli.ts b/packages/playwright-test/src/cli.ts index 74b3bd4723..2d7cfeb4f3 100644 --- a/packages/playwright-test/src/cli.ts +++ b/packages/playwright-test/src/cli.ts @@ -239,8 +239,6 @@ function restartWithExperimentalTsEsm(configFile: string | null): boolean { return false; if (process.env.PW_TS_ESM_ON) return false; - if (!configFile.endsWith('.ts')) - return false; if (!fileIsModule(configFile)) return false; const NODE_OPTIONS = (process.env.NODE_OPTIONS || '') + ` --experimental-loader=${url.pathToFileURL(require.resolve('@playwright/test/lib/experimentalLoader')).toString()}`; diff --git a/packages/playwright-test/src/transform.ts b/packages/playwright-test/src/transform.ts index b7c4208a97..35cbb2b1a5 100644 --- a/packages/playwright-test/src/transform.ts +++ b/packages/playwright-test/src/transform.ts @@ -97,9 +97,7 @@ export function resolveHook(filename: string, specifier: string): string | undef if (builtins.has(specifier)) return; const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx'); - if (!isTypeScript) - return; - const tsconfig = loadAndValidateTsconfigForFile(filename); + const tsconfig = isTypeScript ? loadAndValidateTsconfigForFile(filename) : undefined; if (tsconfig && !isRelativeSpecifier(specifier)) { let longestPrefixLength = -1; let pathMatchedByLongestPrefix: string | undefined; diff --git a/tests/playwright-test/loader.spec.ts b/tests/playwright-test/loader.spec.ts index e01aae4567..8d88ac8ec1 100644 --- a/tests/playwright-test/loader.spec.ts +++ b/tests/playwright-test/loader.spec.ts @@ -216,7 +216,9 @@ test('should load esm config files', async ({ runInlineTest }) => { expect(result.passed).toBe(1); }); -test('should fail to load ts from esm when package.json has type module', async ({ runInlineTest }) => { +test('should load ts from esm when package.json has type module', async ({ runInlineTest, nodeVersion }) => { + // We only support experimental esm mode on Node 16+ + test.skip(nodeVersion.major < 16); const result = await runInlineTest({ 'playwright.config.js': ` //@no-header @@ -225,19 +227,24 @@ test('should fail to load ts from esm when package.json has type module', async `, 'package.json': JSON.stringify({ type: 'module' }), 'a.test.js': ` - import { foo } from './b.ts'; - const { test } = pwt; + //@no-header + import { test, expect } from '@playwright/test'; + import { bar } from './bar.js'; test('check project name', ({}, testInfo) => { expect(testInfo.project.name).toBe('foo'); }); `, - 'b.ts': ` + 'bar.ts': ` + import { foo } from './foo.js'; + export const bar = foo; + `, + 'foo.ts': ` export const foo: string = 'foo'; ` }); - expect(result.exitCode).toBe(1); - expect(result.output).toContain('Unknown file extension ".ts"'); + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); }); test('should filter stack trace for simple expect', async ({ runInlineTest }) => {