From d83608e916aec1bdb7065f116df2588686fbb42d Mon Sep 17 00:00:00 2001 From: Arun Vijo Tharakan Date: Mon, 21 Jul 2025 00:43:20 +0530 Subject: [PATCH] test(estree): use absolute tsconfigRootDir in tests --- .../tests/lib/getProjectConfigFiles.test.ts | 23 ++++----- replace-tsconfig-paths.js | 51 +++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 replace-tsconfig-paths.js diff --git a/packages/typescript-estree/tests/lib/getProjectConfigFiles.test.ts b/packages/typescript-estree/tests/lib/getProjectConfigFiles.test.ts index 8e464eb746fc..e3011a15c1e3 100644 --- a/packages/typescript-estree/tests/lib/getProjectConfigFiles.test.ts +++ b/packages/typescript-estree/tests/lib/getProjectConfigFiles.test.ts @@ -19,7 +19,7 @@ vi.mock(import('node:fs'), async importOriginal => { const parseSettings = { filePath: './repos/repo/packages/package/file.ts', tsconfigMatchCache: new ExpiringCache(1), - tsconfigRootDir: './repos/repo', + tsconfigRootDir: path.resolve(__dirname, './repos/repo'), // ✅ FIXED }; describe(getProjectConfigFiles, () => { @@ -78,23 +78,20 @@ describe(getProjectConfigFiles, () => { const tsconfigMatchCache = new ExpiringCache(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, ); @@ -110,23 +107,20 @@ describe(getProjectConfigFiles, () => { const tsconfigMatchCache = new ExpiringCache(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, ); @@ -165,7 +159,7 @@ 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')}'.]`, ); }); @@ -173,7 +167,10 @@ describe(getProjectConfigFiles, () => { 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 '/'.]`, ); diff --git a/replace-tsconfig-paths.js b/replace-tsconfig-paths.js new file mode 100644 index 000000000000..7ba9ac29810b --- /dev/null +++ b/replace-tsconfig-paths.js @@ -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}`); + } +} 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