Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test runner): disregard native typescript execution in Node.js (#35300)

Dmitry Gozman7cada0322a41
diff --git a/packages/playwright/src/transform/esmLoader.ts b/packages/playwright/src/transform/esmLoader.ts
index 6782d7c0a5..25c3ab2845 100644
--- a/packages/playwright/src/transform/esmLoader.ts
+++ b/packages/playwright/src/transform/esmLoader.ts
@@ -40,12 +40,23 @@ async function resolve(specifier: string, context: { parentURL?: string }, defau
return result;
}
+// non-js files have undefined
+// some js files have null
+// {module/commonjs}-typescript are changed to {module,commonjs} because we handle typescript ourselves
+const kSupportedFormats = new Map([
+ ['commonjs', 'commonjs'],
+ ['module', 'module'],
+ ['commonjs-typescript', 'commonjs'],
+ ['module-typescript', 'module'],
+ [null, null],
+ [undefined, undefined]
+]);
+
// Node < 18.6: defaultLoad takes 3 arguments.
// Node >= 18.6: nextLoad from the chain takes 2 arguments.
async function load(moduleUrl: string, context: { format?: string }, defaultLoad: Function) {
// Bail out for wasm, json, etc.
- // non-js files have context.format === undefined
- if (context.format !== 'commonjs' && context.format !== 'module' && context.format !== undefined)
+ if (!kSupportedFormats.has(context.format))
return defaultLoad(moduleUrl, context, defaultLoad);
// Bail for built-in modules.
@@ -67,7 +78,7 @@ async function load(moduleUrl: string, context: { format?: string }, defaultLoad
// Output format is required, so we determine it manually when unknown.
// shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
return {
- format: context.format || (fileIsModule(filename) ? 'module' : 'commonjs'),
+ format: kSupportedFormats.get(context.format) || (fileIsModule(filename) ? 'module' : 'commonjs'),
source: transformed.code,
shortCircuit: true,
};
diff --git a/tests/playwright-test/esm.spec.ts b/tests/playwright-test/esm.spec.ts
index 53e9d1e0dc..9f567b0f44 100644
--- a/tests/playwright-test/esm.spec.ts
+++ b/tests/playwright-test/esm.spec.ts
@@ -62,7 +62,7 @@ test('should support import attributes', async ({ runInlineTest }) => {
expect(result.stdout).toContain('imported value (test): bar');
});
-test('should import esm from ts when package.json has type module in experimental mode', async ({ runInlineTest }) => {
+test('should import esm from ts when package.json has type module', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
import * as fs from 'fs';
@@ -73,8 +73,10 @@ test('should import esm from ts when package.json has type module in experimenta
import { foo } from './b.ts';
import { bar } from './c.js';
import { qux } from './d.js';
- import { test, expect } from '@playwright/test';
+ // Make sure to import a type-only Locator below to check that our babel strips it out.
+ import { test, expect, Locator } from '@playwright/test';
test('check project name', ({}, testInfo) => {
+ const locator: Locator = null!;
expect(testInfo.project.name).toBe('foo');
expect(bar).toBe('bar');
expect(qux).toBe('qux');