-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerate-test-summary-jest.sh
More file actions
128 lines (107 loc) · 4.44 KB
/
generate-test-summary-jest.sh
File metadata and controls
128 lines (107 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
set -e
# Generate Detailed Test Summary from Multiple Jest JSON Output Files
# Shows breakdown by test type (unit vs integration)
# Usage: ./generate-test-summary-jest.sh <unit-json> <integration-json>
# Guard: skip if GITHUB_STEP_SUMMARY is not set
if [ -z "$GITHUB_STEP_SUMMARY" ]; then
echo "Warning: GITHUB_STEP_SUMMARY not set, skipping summary generation"
exit 0
fi
UNIT_JSON="${1:-}"
INTEGRATION_JSON="${2:-}"
echo "## Test Results" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
# Function to parse Jest JSON file
parse_json() {
local json_file="$1"
local test_type="$2"
if [ ! -f "$json_file" ]; then
echo "0 0 0 0"
return
fi
if command -v jq &> /dev/null; then
# Use jq if available (preferred)
total_tests=$(jq -r '.numTotalTests // 0' "$json_file")
passed=$(jq -r '.numPassedTests // 0' "$json_file")
failed=$(jq -r '.numFailedTests // 0' "$json_file")
skipped=$(jq -r '.numPendingTests // 0' "$json_file")
else
# Fallback to grep/sed if jq is not available
total_tests=$(grep -oP '"numTotalTests":\s*\K[0-9]+' "$json_file" | head -1)
passed=$(grep -oP '"numPassedTests":\s*\K[0-9]+' "$json_file" | head -1)
failed=$(grep -oP '"numFailedTests":\s*\K[0-9]+' "$json_file" | head -1)
skipped=$(grep -oP '"numPendingTests":\s*\K[0-9]+' "$json_file" | head -1)
fi
# Default to 0 if values are empty
total_tests=${total_tests:-0}
passed=${passed:-0}
failed=${failed:-0}
skipped=${skipped:-0}
echo "$total_tests $passed $failed $skipped"
}
# Parse both files
read -r unit_tests unit_passed unit_failed unit_skipped <<< "$(parse_json "$UNIT_JSON" "Unit")"
read -r int_tests int_passed int_failed int_skipped <<< "$(parse_json "$INTEGRATION_JSON" "Integration")"
# Calculate totals
total_tests=$((unit_tests + int_tests))
total_passed=$((unit_passed + int_passed))
total_failed=$((unit_failed + int_failed))
total_skipped=$((unit_skipped + int_skipped))
# Display detailed breakdown
echo "### Summary by Test Type" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "| Test Type | Passed | Failed | Skipped | Total |" >> "$GITHUB_STEP_SUMMARY"
echo "|-----------|--------|--------|---------|-------|" >> "$GITHUB_STEP_SUMMARY"
if [ -f "$UNIT_JSON" ]; then
echo "| 🔧 Unit Tests | $unit_passed | $unit_failed | $unit_skipped | $unit_tests |" >> "$GITHUB_STEP_SUMMARY"
fi
if [ -f "$INTEGRATION_JSON" ]; then
echo "| 🔗 Integration Tests | $int_passed | $int_failed | $int_skipped | $int_tests |" >> "$GITHUB_STEP_SUMMARY"
fi
echo "| **Total** | **$total_passed** | **$total_failed** | **$total_skipped** | **$total_tests** |" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
# Overall status
echo "### Overall Status" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "| Status | Count |" >> "$GITHUB_STEP_SUMMARY"
echo "|--------|-------|" >> "$GITHUB_STEP_SUMMARY"
echo "| ✅ Passed | $total_passed |" >> "$GITHUB_STEP_SUMMARY"
echo "| ❌ Failed | $total_failed |" >> "$GITHUB_STEP_SUMMARY"
echo "| ⏭️ Skipped | $total_skipped |" >> "$GITHUB_STEP_SUMMARY"
echo "| **Total** | **$total_tests** |" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
# List failed tests if any
if [ $total_failed -gt 0 ]; then
echo "### ❌ Failed Tests" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
failed_tests_file=$(mktemp)
# Extract failed tests from both files
for json_file in "$UNIT_JSON" "$INTEGRATION_JSON"; do
if [ -f "$json_file" ]; then
if command -v jq &> /dev/null; then
jq -r '.testResults[]? | select(.status == "failed") | .assertionResults[]? | select(.status == "failed") | "\(.ancestorTitles | join(" > ")) > \(.title)"' "$json_file" >> "$failed_tests_file" 2>/dev/null || true
else
# Basic fallback without jq
grep -oP '"fullName":\s*"\K[^"]*' "$json_file" | while read -r line; do
if echo "$line" | grep -q "failed"; then
echo "$line" >> "$failed_tests_file"
fi
done 2>/dev/null || true
fi
fi
done
if [ -s "$failed_tests_file" ]; then
while IFS= read -r test; do
echo "- \`$test\`" >> "$GITHUB_STEP_SUMMARY"
done < "$failed_tests_file"
else
echo "_Unable to parse individual test names_" >> "$GITHUB_STEP_SUMMARY"
fi
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "❌ **Tests failed!**" >> "$GITHUB_STEP_SUMMARY"
rm -f "$failed_tests_file"
exit 1
else
echo "✅ **All tests passed!**" >> "$GITHUB_STEP_SUMMARY"
fi