feat(test runner): support jsconfig.json's baseUrl+paths (#19219)

Fixes #19129.
This commit is contained in:
Dmitry Gozman 2022-12-01 16:42:25 -08:00 committed by GitHub
parent 4fbd8b9672
commit 6471e8536e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 5 deletions

View file

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

View file

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