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.6",
"version": "0.2.7",
"description": "TestSolar support for jest tool",
"main": "main.js",
"scripts": {
Expand Down
44 changes: 37 additions & 7 deletions jest/src/jestx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,61 @@ 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
const singleItMatch = line.match(/^(it|test)\(['"](.*?)['"],/);
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);
}
}
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.6'
version: '0.2.7'
defaultBaseImage: node:18
langType: INTERPRETED
description: |-
Expand Down