From 5e1c61d49ac9d40ae66a86569c6f44b4e8a9f655 Mon Sep 17 00:00:00 2001 From: ambzhang Date: Fri, 19 Sep 2025 15:57:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test[utils]:=20=E6=9B=B4=E6=96=B0=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=87=BD=E6=95=B0=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改工具函数实现并同步更新对应的单元测试,确保功能一致性。 --- jest/src/jestx/utils.ts | 37 ++++++++++++++++++++++++++++++++++--- jest/tests/utils.test.ts | 21 +++++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) 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); From f274b967fd7a1c8fc0ab0f5a3449c4025c6d59bf Mon Sep 17 00:00:00 2001 From: ambzhang Date: Fri, 19 Sep 2025 15:59:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore(jest):=20=E6=9B=B4=E6=96=B0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=B7=A5=E5=85=B7=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新 jest 的 package.json 和 testtool.yaml 配置文件以保持一致性。 --- jest/package.json | 2 +- jest/testtool.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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: |-