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