Skip to content

Commit 7cada03

Browse files
authored
fix(test runner): disregard native typescript execution in Node.js (#35300)
1 parent c018b71 commit 7cada03

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/playwright/src/transform/esmLoader.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ async function resolve(specifier: string, context: { parentURL?: string }, defau
4040
return result;
4141
}
4242

43+
// non-js files have undefined
44+
// some js files have null
45+
// {module/commonjs}-typescript are changed to {module,commonjs} because we handle typescript ourselves
46+
const kSupportedFormats = new Map([
47+
['commonjs', 'commonjs'],
48+
['module', 'module'],
49+
['commonjs-typescript', 'commonjs'],
50+
['module-typescript', 'module'],
51+
[null, null],
52+
[undefined, undefined]
53+
]);
54+
4355
// Node < 18.6: defaultLoad takes 3 arguments.
4456
// Node >= 18.6: nextLoad from the chain takes 2 arguments.
4557
async function load(moduleUrl: string, context: { format?: string }, defaultLoad: Function) {
4658
// Bail out for wasm, json, etc.
47-
// non-js files have context.format === undefined
48-
if (context.format !== 'commonjs' && context.format !== 'module' && context.format !== undefined)
59+
if (!kSupportedFormats.has(context.format))
4960
return defaultLoad(moduleUrl, context, defaultLoad);
5061

5162
// Bail for built-in modules.
@@ -67,7 +78,7 @@ async function load(moduleUrl: string, context: { format?: string }, defaultLoad
6778
// Output format is required, so we determine it manually when unknown.
6879
// shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
6980
return {
70-
format: context.format || (fileIsModule(filename) ? 'module' : 'commonjs'),
81+
format: kSupportedFormats.get(context.format) || (fileIsModule(filename) ? 'module' : 'commonjs'),
7182
source: transformed.code,
7283
shortCircuit: true,
7384
};

tests/playwright-test/esm.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test('should support import attributes', async ({ runInlineTest }) => {
6262
expect(result.stdout).toContain('imported value (test): bar');
6363
});
6464

65-
test('should import esm from ts when package.json has type module in experimental mode', async ({ runInlineTest }) => {
65+
test('should import esm from ts when package.json has type module', async ({ runInlineTest }) => {
6666
const result = await runInlineTest({
6767
'playwright.config.ts': `
6868
import * as fs from 'fs';
@@ -73,8 +73,10 @@ test('should import esm from ts when package.json has type module in experimenta
7373
import { foo } from './b.ts';
7474
import { bar } from './c.js';
7575
import { qux } from './d.js';
76-
import { test, expect } from '@playwright/test';
76+
// Make sure to import a type-only Locator below to check that our babel strips it out.
77+
import { test, expect, Locator } from '@playwright/test';
7778
test('check project name', ({}, testInfo) => {
79+
const locator: Locator = null!;
7880
expect(testInfo.project.name).toBe('foo');
7981
expect(bar).toBe('bar');
8082
expect(qux).toBe('qux');

0 commit comments

Comments
 (0)