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): resolve tsconfig extends to a bare package name (#43240)

Pavel Feldman77af5578f89c
diff --git a/packages/playwright/src/transform/tsconfig-loader.ts b/packages/playwright/src/transform/tsconfig-loader.ts
index 1d040edc39..b9f71b5fad 100644
--- a/packages/playwright/src/transform/tsconfig-loader.ts
+++ b/packages/playwright/src/transform/tsconfig-loader.ts
@@ -72,18 +72,36 @@ export function loadTsConfig(configPath: string): LoadedTsConfig[] {
}
function resolveConfigFile(baseConfigFile: string, referencedConfigFile: string) {
- if (!referencedConfigFile.endsWith('.json'))
- referencedConfigFile += '.json';
const currentDir = path.dirname(baseConfigFile);
- let resolvedConfigFile = path.resolve(currentDir, referencedConfigFile);
- if (referencedConfigFile.includes('/') && referencedConfigFile.includes('.') && !fs.existsSync(resolvedConfigFile))
- resolvedConfigFile = path.join(currentDir, 'node_modules', referencedConfigFile);
+ const referencedFile = referencedConfigFile.endsWith('.json') ? referencedConfigFile : referencedConfigFile + '.json';
+ const resolvedConfigFile = path.resolve(currentDir, referencedFile);
+ if (fs.existsSync(resolvedConfigFile) || referencedConfigFile.startsWith('.') || path.isAbsolute(referencedConfigFile))
+ return resolvedConfigFile;
+
+ // A bare specifier names a package in the config's node_modules, like tsc resolves it:
+ // a file inside the package, or the package itself through the "tsconfig" field of its
+ // package.json or the tsconfig.json at its root.
+ const packageDir = path.join(currentDir, 'node_modules', referencedConfigFile);
+ const candidates = [path.join(currentDir, 'node_modules', referencedFile)];
+ const packageTsconfig = packageJsonTsconfigField(path.join(packageDir, 'package.json'));
+ if (packageTsconfig)
+ candidates.push(path.resolve(packageDir, packageTsconfig));
+ candidates.push(path.join(packageDir, 'tsconfig.json'));
// Note: this function may return a non-existing file, and the caller silently ignores it.
// We deliberately do not throw in this case, because we do not want to repeat the whole
// resolution process that tsc has, e.g. node_modules walk-up and package.json "exports".
// See https://github.com/microsoft/playwright/issues/41989.
// TODO: implement tsc-compatible resolution and start throwing on invalid "extends"/"references".
- return resolvedConfigFile;
+ return candidates.find(candidate => fs.existsSync(candidate)) ?? resolvedConfigFile;
+}
+
+function packageJsonTsconfigField(packageJsonFile: string): string | undefined {
+ try {
+ const tsconfig = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8')).tsconfig;
+ return typeof tsconfig === 'string' ? tsconfig : undefined;
+ } catch {
+ return undefined;
+ }
}
function innerLoadTsConfig(
diff --git a/tests/playwright-test/resolver.spec.ts b/tests/playwright-test/resolver.spec.ts
index 6215abb16b..ee1ef144e3 100644
--- a/tests/playwright-test/resolver.spec.ts
+++ b/tests/playwright-test/resolver.spec.ts
@@ -625,6 +625,77 @@ test('should resolve extends from an explicit node_modules subpath', async ({ ru
expect(result.exitCode).toBe(0);
});
+test('should resolve extends from a bare package name', async ({ runInlineTest }) => {
+ test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43234' });
+
+ // The @tsconfig/* base packages are documented as "extends": "@tsconfig/node20".
+ // tsc resolves a bare package name to the tsconfig.json at the package root.
+ const result = await runInlineTest({
+ 'package.json': JSON.stringify({ name: 'test-project' }),
+ 'node_modules/@tsconfig/base/package.json': JSON.stringify({ name: '@tsconfig/base', version: '1.0.0' }),
+ 'node_modules/@tsconfig/base/tsconfig.json': `{
+ "compilerOptions": {
+ "paths": {
+ "util/*": ["./mapped/*"],
+ },
+ },
+ }`,
+ 'tsconfig.json': `{
+ "extends": "@tsconfig/base",
+ "compilerOptions": {
+ "baseUrl": ".",
+ },
+ }`,
+ 'a.test.ts': `
+ import { foo } from 'util/file';
+ import { test, expect } from '@playwright/test';
+ test('test', () => {
+ expect(foo).toBe('foo');
+ });
+ `,
+ 'mapped/file.ts': `
+ export const foo = 'foo';
+ `,
+ });
+ expect(result.passed).toBe(1);
+ expect(result.exitCode).toBe(0);
+});
+
+test('should resolve extends from the "tsconfig" field in package.json', async ({ runInlineTest }) => {
+ test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43234' });
+
+ const result = await runInlineTest({
+ 'package.json': JSON.stringify({ name: 'test-project' }),
+ 'node_modules/@my/tsconfig-base/package.json': JSON.stringify({ name: '@my/tsconfig-base', version: '1.0.0', tsconfig: './base.json' }),
+ 'node_modules/@my/tsconfig-base/base.json': `{
+ "compilerOptions": {
+ "paths": {
+ "util/*": ["./mapped/*"],
+ },
+ },
+ }`,
+ 'tsconfig.json': `{
+ "extends": "@my/tsconfig-base",
+ "compilerOptions": {
+ "baseUrl": ".",
+ },
+ }`,
+ 'a.test.ts': `
+ import { foo } from 'util/file';
+ import { test, expect } from '@playwright/test';
+ test('test', () => {
+ expect(foo).toBe('foo');
+ });
+ `,
+ 'mapped/file.ts': `
+ export const foo = 'foo';
+ `,
+ });
+
+ expect(result.passed).toBe(1);
+ expect(result.exitCode).toBe(0);
+});
+
test('should ignore extends bare specifier resolvable only via node_modules walk-up', async ({ runInlineTest }, testInfo) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/41989' });