Skip to content

Commit

Permalink
cherry-pick(#16395): fix(test runner): do not resolve relative import…
Browse files Browse the repository at this point in the history
…s through baseUrl (#16415)

SHA: f58c376
  • Loading branch information
mxschmitt committed Aug 10, 2022
1 parent 7de99f3 commit 0227708
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/playwright-test/src/transform.ts
Expand Up @@ -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;

Expand Down Expand Up @@ -258,3 +258,7 @@ export function belongsToNodeModules(file: string) {
return true;
return false;
}

function isRelativeSpecifier(specifier: string) {
return specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../');
}
21 changes: 19 additions & 2 deletions tests/playwright-test/resolver.spec.ts
Expand Up @@ -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": {
Expand All @@ -309,20 +309,37 @@ 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);
});
`,
});

expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
expect(result.output).not.toContain(`Could not`);
expect(result.output).not.toContain(`Cannot`);
});

0 comments on commit 0227708

Please sign in to comment.