Skip to content
Merged
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
28 changes: 24 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,30 @@ export default function (originOpts: any = {}) {

let config = getConfig();

// Merge `jest.config.js`
const userJestConfigFile = path.resolve(cwd, 'jest.config.js');
if (fs.existsSync(userJestConfigFile)) {
const { setupFiles = [], setupFilesAfterEnv = [], ...restConfig } = require(userJestConfigFile);
// Merge Jest config file (support .js, .ts, .json)
let userJestConfigFile = null;
const possibleConfigFiles = [
'jest.config.js',
'jest.config.ts',
'jest.config.json',
'.jest.config.js',
'.jest.config.ts',
'.jest.config.json',
];

for (let i = 0; i < possibleConfigFiles.length; i++) {
const configPath = path.resolve(cwd, possibleConfigFiles[i]);
if (fs.existsSync(configPath)) {
userJestConfigFile = configPath;
break;
}
}
Comment on lines +67 to +84
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# 检查 package.json 中的依赖和脚本
cat package.json | head -50

Repository: react-component/rc-test

Length of output: 1386


🏁 Script executed:

# 搜索是否存在 TypeScript 加载器相关配置
rg "ts-node|@swc-node/register|tsx|esbuild-register|register" -t json -t js -t ts --max-count 20

Repository: react-component/rc-test

Length of output: 141


🏁 Script executed:

# 查看 src/index.ts 完整实现(第 67-97 行)
sed -n '60,100p' src/index.ts

Repository: react-component/rc-test

Length of output: 1174


🏁 Script executed:

# 检查是否存在 jest.config.ts 或 .jest.config.ts 文件
fd -t f "^\.?jest\.config\.(ts|js|json)$" --max-depth 2

Repository: react-component/rc-test

Length of output: 81


🏁 Script executed:

# 获取完整的 package.json
cat package.json

Repository: react-component/rc-test

Length of output: 2062


🏁 Script executed:

# 检查 bin/rc-test.js 是否有 TypeScript 加载器注册
cat bin/rc-test.js

Repository: react-component/rc-test

Length of output: 438


🏁 Script executed:

# 检查是否有 tsconfig.json
cat tsconfig.json 2>/dev/null || echo "no tsconfig.json found"

Repository: react-component/rc-test

Length of output: 89


🏁 Script executed:

# 检查编译后的 lib/index.js 文件是否存在及其内容
head -100 lib/index.js | grep -A 20 "possibleConfigFiles"

Repository: react-component/rc-test

Length of output: 138


🏁 Script executed:

# 验证是否有任何 TypeScript 加载器在 node_modules 或项目中
rg "ts-node|@swc-node|tsx|esbuild-register" src/ bin/ --type ts --type js

Repository: react-component/rc-test

Length of output: 141


处理 TypeScript 配置文件的加载支持。

代码在第 70 和 74 行列出了 jest.config.ts.jest.config.ts 作为可能的配置文件,但第 87 行使用 require() 加载配置文件。项目的依赖中不包含 ts-node、@swc-node/register、tsx 或其他 TypeScript 加载器,因此当用户提供 TypeScript 配置文件时,require() 会失败。

建议:

  • 移除 .ts 配置文件选项,或
  • 在运行时添加 TypeScript 加载器支持,或
  • .ts 文件添加错误处理并改用 .js 配置
🤖 Prompt for AI Agents
In src/index.ts around lines 67 to 84, the code lists jest.config.ts and
.jest.config.ts as possible config files but later uses require() to load the
chosen file, which will fail for TypeScript files without a runtime loader;
remove the .ts entries from possibleConfigFiles (only keep .js and .json
variants) or add detection for .ts files and handle them explicitly by either
(a) attempting to resolve a same-named .js/.json fallback and using that, or (b)
loading a TypeScript runtime (e.g., require('ts-node/register') or similar)
before require() — implement the simpler approach of removing .ts from the list
and, if you prefer to keep .ts support, add a clear error message that instructs
the user to install a TypeScript loader when a .ts config is found.

Comment on lines +68 to +84

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了提高代码的可读性和简洁性,您可以使用 Array.prototype.mapArray.prototype.find 来查找存在的配置文件,以替代传统的 for 循环。这样可以使代码更具声明性,并且可以用一个常量声明代替 let

  const userJestConfigFile = possibleConfigFiles
    .map(configFile => path.resolve(cwd, configFile))
    .find(configPath => fs.existsSync(configPath));


if (userJestConfigFile) {
let userConfig = require(userJestConfigFile);
userConfig = userConfig.default || userConfig;

const { setupFiles = [], setupFilesAfterEnv = [], ...restConfig } = userConfig;
config = {
...config,
...restConfig,
Expand Down