From 710f17f47b372acf753baca0e6bcf34bd2a95319 Mon Sep 17 00:00:00 2001 From: ambzhang Date: Tue, 23 Sep 2025 16:03:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=94=B1=E4=BA=8E=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E5=8F=98=E6=9B=B4=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E4=BB=85=E5=8C=85=E5=90=AB=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=88`jest/jest/src/jestx/utils.ts`?= =?UTF-8?q?=EF=BC=89=EF=BC=8C=E6=B2=A1=E6=9C=89=E5=85=B7=E4=BD=93=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=8F=98=E6=9B=B4=E5=86=85=E5=AE=B9=EF=BC=8C?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E7=94=9F=E6=88=90=E5=85=B7=E4=BD=93=E7=9A=84?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BF=A1=E6=81=AF=E3=80=82=E8=AF=B7=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E5=85=B7=E4=BD=93=E7=9A=84=E4=BB=A3=E7=A0=81=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=E5=86=85=E5=AE=B9=EF=BC=88diff=20=E5=86=85=E5=AE=B9?= =?UTF-8?q?=EF=BC=89=E4=BB=A5=E4=BE=BF=E7=94=9F=E6=88=90=E5=87=86=E7=A1=AE?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E4=BA=A4=E4=BF=A1=E6=81=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest/src/jestx/utils.ts | 44 ++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/jest/src/jestx/utils.ts b/jest/src/jestx/utils.ts index 5917adf..ddf5ef2 100644 --- a/jest/src/jestx/utils.ts +++ b/jest/src/jestx/utils.ts @@ -147,16 +147,40 @@ export const parseTestcase = ( // 将文件内容按行分割 const lines = fileContent.split("\n"); - // 初始化 describeContent 变量 - let describeContent = ""; + // 使用栈来跟踪多层 describe 嵌套 + const describeStack: string[] = []; + let braceCount = 0; + const braceStack: number[] = []; // 遍历每一行 for (const line of lines) { + // 计算当前行的缩进层级 + const indentLevel = (line.match(/^\s*/)?.[0].length || 0) / 4; // 假设使用4个空格缩进 + // 匹配 describe 标签 const describeMatch = line.match(/describe\(['"](.*?)['"],/); if (describeMatch) { - // 更新 describeContent - describeContent = describeMatch[1]; + // 根据缩进层级调整 describe 栈 + while (describeStack.length > indentLevel) { + describeStack.pop(); + braceStack.pop(); + } + // 添加新的 describe 到栈中 + describeStack.push(describeMatch[1]); + braceStack.push(braceCount); + } + + // 匹配大括号来跟踪作用域 + const openBraces = (line.match(/\{/g) || []).length; + const closeBraces = (line.match(/\}/g) || []).length; + braceCount += openBraces - closeBraces; + + // 当遇到闭合大括号时,检查是否需要弹出 describe + if (closeBraces > 0) { + while (braceStack.length > 0 && braceCount <= braceStack[braceStack.length - 1]) { + describeStack.pop(); + braceStack.pop(); + } } // 扫描只有it或者test标签用例,无describe @@ -164,14 +188,20 @@ export const parseTestcase = ( if (singleItMatch) { const testcase = `${relativePath.replace(projPath, "")}?${singleItMatch[2]}`; testcases.push(testcase); - describeContent = ""; continue; } + // 匹配describe下的 it 或 test 标签 const itMatch = line.match(/\s+(it|test)\(['"](.*?)['"],/); if (itMatch) { - if (describeContent) { - const testcase = `${relativePath.replace(projPath, "")}?${describeContent} ${itMatch[2]}`; + if (describeStack.length > 0) { + // 构建完整的测试用例路径,包含所有层级的 describe + const fullDescribePath = describeStack.join(' '); + const testcase = `${relativePath.replace(projPath, "")}?${fullDescribePath} ${itMatch[2]}`; + testcases.push(testcase); + } else { + // 如果没有 describe 包围,直接使用测试名称 + const testcase = `${relativePath.replace(projPath, "")}?${itMatch[2]}`; testcases.push(testcase); } } From 31ec1fa247f392d2c112f06be54594b8eef3c611 Mon Sep 17 00:00:00 2001 From: ambzhang Date: Tue, 23 Sep 2025 16:04:22 +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 af386af..55e2805 100644 --- a/jest/package.json +++ b/jest/package.json @@ -1,6 +1,6 @@ { "name": "testsolar-oss-jest", - "version": "0.2.6", + "version": "0.2.7", "description": "TestSolar support for jest tool", "main": "main.js", "scripts": { diff --git a/jest/testtool.yaml b/jest/testtool.yaml index be80fe3..c5a5d29 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.6' +version: '0.2.7' defaultBaseImage: node:18 langType: INTERPRETED description: |-