diff --git a/jest/package.json b/jest/package.json index e03f2bd..af386af 100644 --- a/jest/package.json +++ b/jest/package.json @@ -1,6 +1,6 @@ { "name": "testsolar-oss-jest", - "version": "0.2.5", + "version": "0.2.6", "description": "TestSolar support for jest tool", "main": "main.js", "scripts": { diff --git a/jest/src/jestx/utils.ts b/jest/src/jestx/utils.ts index aa1fca2..5917adf 100644 --- a/jest/src/jestx/utils.ts +++ b/jest/src/jestx/utils.ts @@ -311,8 +311,19 @@ export async function executeCommands( try { await retry(async () => { - await executeCommand(command); + try { + await executeCommand(command); + } catch (commandError) { + // 命令执行失败时,立即检查 JSON 文件是否存在 + if (jsonName && fs.existsSync(jsonName)) { + log.info(`Jest command failed but JSON file exists: ${jsonName}, will use existing results`); + return; // 不抛出错误,让重试机制停止 + } + // 如果 JSON 文件不存在,重新抛出错误以触发重试 + throw commandError; + } + // 命令执行成功后,检查文件是否存在 if (!fs.existsSync(jsonName)) { log.error(`File not found after command execution: ${jsonName}`); throw new Error(`File not found: ${jsonName}`); @@ -329,14 +340,34 @@ export async function executeCommands( }, }); - const testResults = parseJsonFile(projPath, jsonName); - Object.assign(results, testResults); + // 解析 JSON 文件(无论是命令成功还是文件已存在) + if (fs.existsSync(jsonName)) { + const testResults = parseJsonFile(projPath, jsonName); + Object.assign(results, testResults); + } } catch (finalError) { + // 最终失败处理:检查 JSON 文件是否存在 + if (jsonName && fs.existsSync(jsonName)) { + log.info(`Jest execution failed after retries but JSON file exists: ${jsonName}, parsing results from existing file`); + try { + const testResults = parseJsonFile(projPath, jsonName); + Object.assign(results, testResults); + return results; + } catch (parseError) { + log.error(`Failed to parse existing JSON file ${jsonName}:`, parseError); + // 如果解析失败,继续抛出原始错误 + } + } else if (jsonName) { + log.error(`Jest execution failed and specified JSON file does not exist: ${jsonName}`); + throw new Error(`Jest execution failed and specified JSON file does not exist: ${jsonName}`); + } + if (finalError instanceof Error) { log.error(`Failed to execute command after retries: ${finalError.message}`); } else { log.error(`Failed to execute command after retries: ${finalError}`); } + throw finalError; } return results; diff --git a/jest/tests/utils.test.ts b/jest/tests/utils.test.ts index 35568b7..d0c41b7 100644 --- a/jest/tests/utils.test.ts +++ b/jest/tests/utils.test.ts @@ -308,8 +308,25 @@ describe("executeCommands", () => { const command = `touch ${jsonName}`; // 简单的命令来创建 jsonName 文件 test("should execute command and parse JSON file successfully", async () => { - // 创建一个空的 results.json 文件 - fs.writeFileSync(jsonName, JSON.stringify({})); + // 创建一个包含有效 testResults 的 JSON 文件 + const validJsonData = { + testResults: [ + { + assertionResults: [ + { + fullName: "test case", + status: "passed", + failureMessages: [] + } + ], + endTime: Date.now(), + message: "", + name: path.join(projPath, "test.js"), + startTime: Date.now() - 1000 + } + ] + }; + fs.writeFileSync(jsonName, JSON.stringify(validJsonData)); const results = await executeCommands(projPath, command, jsonName); diff --git a/jest/testtool.yaml b/jest/testtool.yaml index d5522a1..be80fe3 100644 --- a/jest/testtool.yaml +++ b/jest/testtool.yaml @@ -2,7 +2,7 @@ schemaVersion: 1.0 name: jest nameZh: Jest自动化测试 lang: javascript -version: '0.2.5' +version: '0.2.6' defaultBaseImage: node:18 langType: INTERPRETED description: |-