Skip to content

test(typescript-estree): use absolute tsconfigRootDir in tests #11415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions packages/typescript-estree/tests/lib/getProjectConfigFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vi.mock(import('node:fs'), async importOriginal => {
const parseSettings = {
filePath: './repos/repo/packages/package/file.ts',
tsconfigMatchCache: new ExpiringCache<string, string>(1),
tsconfigRootDir: './repos/repo',
tsconfigRootDir: path.resolve(__dirname, './repos/repo'), // ✅ FIXED
};

describe(getProjectConfigFiles, () => {
Expand Down Expand Up @@ -78,23 +78,20 @@ describe(getProjectConfigFiles, () => {

const tsconfigMatchCache = new ExpiringCache<string, string>(1);

// This should call to fs.existsSync three times: c, b, a
getProjectConfigFiles(
{
filePath: './a/b/c/d.ts',
tsconfigMatchCache,
tsconfigRootDir: './a',
tsconfigRootDir: path.resolve(__dirname, './a'), // ✅ FIXED
},
true,
);

// This should call to fs.existsSync once: e
// Then it should retrieve c from cache, pointing to a
const actual = getProjectConfigFiles(
{
filePath: './a/b/c/e/f.ts',
tsconfigMatchCache,
tsconfigRootDir: './a',
tsconfigRootDir: path.resolve(__dirname, './a'), // ✅ FIXED
},
true,
);
Expand All @@ -110,23 +107,20 @@ describe(getProjectConfigFiles, () => {

const tsconfigMatchCache = new ExpiringCache<string, string>(1);

// This should call to fs.existsSync 4 times: d, c, b, a
getProjectConfigFiles(
{
filePath: './a/b/c/d/e.ts',
tsconfigMatchCache,
tsconfigRootDir: './a',
tsconfigRootDir: path.resolve(__dirname, './a'), // ✅ FIXED
},
true,
);

// This should call to fs.existsSync 2: g, f
// Then it should retrieve b from cache, pointing to a
const actual = getProjectConfigFiles(
{
filePath: './a/b/f/g/h.ts',
tsconfigMatchCache,
tsconfigRootDir: './a',
tsconfigRootDir: path.resolve(__dirname, './a'), // ✅ FIXED
},
true,
);
Expand Down Expand Up @@ -165,15 +159,18 @@ describe(getProjectConfigFiles, () => {
expect(() =>
getProjectConfigFiles(parseSettings, true),
).toThrowErrorMatchingInlineSnapshot(
`[Error: project was set to \`true\` but couldn't find any tsconfig.json relative to './repos/repo/packages/package/file.ts' within './repos/repo'.]`,
`[Error: project was set to \`true\` but couldn't find any tsconfig.json relative to './repos/repo/packages/package/file.ts' within '${path.resolve(__dirname, './repos/repo')}'.]`,
);
});

it('throws when searching passes the tsconfigRootDir', () => {
mockExistsSync.mockReturnValue(false);

expect(() =>
getProjectConfigFiles({ ...parseSettings, tsconfigRootDir: '/' }, true),
getProjectConfigFiles(
{ ...parseSettings, tsconfigRootDir: path.resolve('/') },
true,
),
).toThrowErrorMatchingInlineSnapshot(
`[Error: project was set to \`true\` but couldn't find any tsconfig.json relative to './repos/repo/packages/package/file.ts' within '/'.]`,
);
Expand Down
51 changes: 51 additions & 0 deletions replace-tsconfig-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
const path = require('path');

const testDir = path.join(__dirname, 'packages/typescript-estree/tests/lib');

function walk(dir, filelist = []) {
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
walk(fullPath, filelist);
} else if (file.endsWith('.ts')) {
filelist.push(fullPath);
}
});
return filelist;
}

const files = walk(testDir);

for (const file of files) {
let content = fs.readFileSync(file, 'utf-8');
let updated = content;

// Add import path if not present
if (!updated.includes("path from 'path'")) {
updated = updated.replace(/^(import.*\n)/, `$1import path from 'path';\n`);
}

// Replace tsconfigRootDir: './something' with path.resolve(__dirname, '...')
updated = updated.replace(
/tsconfigRootDir:\s*['"`]([^'"`]+)['"`]/g,
(_, relPath) => `tsconfigRootDir: path.resolve(__dirname, '${relPath}')`,
);

// Replace project: './something'
updated = updated.replace(
/project:\s*['"`]([^'"`]+)['"`]/g,
(_, relPath) => `project: path.resolve(__dirname, '${relPath}')`,
);

// Replace filePath: './something'
updated = updated.replace(
/filePath:\s*['"`]([^'"`]+)['"`]/g,
(_, relPath) => `filePath: path.resolve(__dirname, '${relPath}')`,
);

if (content !== updated) {
fs.writeFileSync(file, updated, 'utf-8');
console.log(`✔ Updated: ${file}`);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy