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

This commit is contained in:
Dmitry Gozman 2025-03-20 19:17:17 +00:00 committed by GitHub
parent c018b71d19
commit 7cada0322a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 5 deletions

View File

@ -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,
};

View File

@ -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');