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): do not resolve relative imports through baseUrl (#16395)

Dmitry Gozmanf58c376443f5
diff --git a/packages/playwright-test/src/transform.ts b/packages/playwright-test/src/transform.ts
index 4d97b8cd14..b7c4208a97 100644
--- a/packages/playwright-test/src/transform.ts
+++ b/packages/playwright-test/src/transform.ts
@@ -100,7 +100,7 @@ export function resolveHook(filename: string, specifier: string): string | undef
if (!isTypeScript)
return;
const tsconfig = loadAndValidateTsconfigForFile(filename);
- if (tsconfig) {
+ if (tsconfig && !isRelativeSpecifier(specifier)) {
let longestPrefixLength = -1;
let pathMatchedByLongestPrefix: string | undefined;
@@ -258,3 +258,7 @@ export function belongsToNodeModules(file: string) {
return true;
return false;
}
+
+function isRelativeSpecifier(specifier: string) {
+ return specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../');
+}
diff --git a/tests/playwright-test/resolver.spec.ts b/tests/playwright-test/resolver.spec.ts
index 0c5443bad5..313139fdf1 100644
--- a/tests/playwright-test/resolver.spec.ts
+++ b/tests/playwright-test/resolver.spec.ts
@@ -298,8 +298,8 @@ test('should not use baseurl for relative imports', async ({ runInlineTest }) =>
});
test('should not use baseurl for relative imports when dir with same name exists', async ({ runInlineTest }) => {
- test.fail();
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/15891' });
+
const result = await runInlineTest({
'frontend/tsconfig.json': `{
"compilerOptions": {
@@ -309,15 +309,31 @@ test('should not use baseurl for relative imports when dir with same name exists
'frontend/src/utils/foo.js': `
export const foo = -1;
`,
+ 'frontend/src/index.js': `
+ export const index = -1;
+ `,
+ 'frontend/src/.bar.js': `
+ export const bar = 42;
+ `,
'frontend/playwright/tests/utils.ts': `
export const foo = 42;
`,
+ 'frontend/playwright/tests/index.js': `
+ export const index = 42;
+ `,
'frontend/playwright/tests/forms_cms_standard.spec.ts': `
- // This relative import should not use baseUrl
+ // These relative imports should not use baseUrl
import { foo } from './utils';
+ import { index } from '.';
+
+ // This absolute import should use baseUrl
+ import { bar } from '.bar';
+
const { test } = pwt;
test('test', ({}, testInfo) => {
expect(foo).toBe(42);
+ expect(index).toBe(42);
+ expect(bar).toBe(42);
});
`,
});
@@ -325,4 +341,5 @@ test('should not use baseurl for relative imports when dir with same name exists
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
expect(result.output).not.toContain(`Could not`);
+ expect(result.output).not.toContain(`Cannot`);
});