Skip to content
Merged
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
2 changes: 1 addition & 1 deletion jest/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
37 changes: 34 additions & 3 deletions jest/src/jestx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand All @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions jest/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion jest/testtool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down