From 6471e8536ef96534b6ff310eaaad5a5952448087 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 1 Dec 2022 16:42:25 -0800 Subject: [PATCH] feat(test runner): support jsconfig.json's baseUrl+paths (#19219) Fixes #19129. --- .../src/third_party/tsconfig-loader.ts | 20 ++++++++--- tests/playwright-test/resolver.spec.ts | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/playwright-test/src/third_party/tsconfig-loader.ts b/packages/playwright-test/src/third_party/tsconfig-loader.ts index c6a0ae2c3e..dead55937a 100644 --- a/packages/playwright-test/src/third_party/tsconfig-loader.ts +++ b/packages/playwright-test/src/third_party/tsconfig-loader.ts @@ -102,9 +102,13 @@ export function walkForTsConfig( directory: string, existsSync: (path: string) => boolean = fs.existsSync ): string | undefined { - const configPath = path.join(directory, "./tsconfig.json"); - if (existsSync(configPath)) { - return configPath; + const tsconfigPath = path.join(directory, "./tsconfig.json"); + if (existsSync(tsconfigPath)) { + return tsconfigPath; + } + const jsconfigPath = path.join(directory, "./jsconfig.json"); + if (existsSync(jsconfigPath)) { + return jsconfigPath; } const parentDirectory = path.join(directory, "../"); @@ -129,7 +133,7 @@ function loadTsconfig( const configString = readFileSync(configFilePath); const cleanedJson = StripBom(configString); - const config: Tsconfig = json5.parse(cleanedJson); + let config: Tsconfig = json5.parse(cleanedJson); let extendedConfig = config.extends; if (extendedConfig) { @@ -166,7 +170,7 @@ function loadTsconfig( ); } - return { + config = { ...base, ...config, compilerOptions: { @@ -175,6 +179,12 @@ function loadTsconfig( }, }; } + + if (path.basename(configFilePath) === 'jsconfig.json' && config.compilerOptions?.allowJs === undefined) { + config.compilerOptions = config.compilerOptions || {}; + config.compilerOptions.allowJs = true; + } + return config; } diff --git a/tests/playwright-test/resolver.spec.ts b/tests/playwright-test/resolver.spec.ts index 1ac8cc2ec3..802896a17a 100644 --- a/tests/playwright-test/resolver.spec.ts +++ b/tests/playwright-test/resolver.spec.ts @@ -398,3 +398,37 @@ test('should not respect path resolver for JS files w/o allowJS', async ({ runIn expect(stripAnsi(result.output)).toContain('Cannot find module \'util/b\''); expect(result.exitCode).toBe(1); }); + +test('should respect path resolver for JS and TS files from jsconfig.json', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': `export default { projects: [{name: 'foo'}], };`, + 'jsconfig.json': `{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "util/*": ["./foo/bar/util/*"], + }, + }, + }`, + 'a.test.js': ` + const { foo } = require('util/b'); + const { test } = pwt; + test('test', ({}, testInfo) => { + expect(testInfo.project.name).toBe(foo); + }); + `, + 'b.test.ts': ` + import { foo } from 'util/b'; + const { test } = pwt; + test('test', ({}, testInfo) => { + expect(testInfo.project.name).toBe(foo); + }); + `, + 'foo/bar/util/b.ts': ` + module.exports = { foo: 'foo' }; + `, + }); + + expect(result.passed).toBe(2); + expect(result.exitCode).toBe(0); +});