1
+ #! /bin/bash
2
+
3
+ # Script to debug why Jest tests hang after completion
4
+
5
+ echo " === Starting Jest test hang debugging ==="
6
+ echo " Expected: Tests should exit within 5 seconds"
7
+ echo " Actual: Tests hang indefinitely"
8
+ echo " "
9
+
10
+ # Run tests with timeout and capture output
11
+ echo " Running tests with 10 second timeout..."
12
+ timeout 10s npm test > test_output.log 2>&1 &
13
+ TEST_PID=$!
14
+
15
+ # Monitor the test process
16
+ sleep 1
17
+ echo " Test process PID: $TEST_PID "
18
+
19
+ # Wait for either completion or timeout
20
+ wait $TEST_PID
21
+ EXIT_CODE=$?
22
+
23
+ echo " "
24
+ echo " === Test Results ==="
25
+ if [ $EXIT_CODE -eq 124 ]; then
26
+ echo " ❌ TESTS TIMED OUT (hung after 10 seconds)"
27
+ else
28
+ echo " ✅ Tests completed normally with exit code: $EXIT_CODE "
29
+ fi
30
+
31
+ echo " "
32
+ echo " === Test Output (last 20 lines) ==="
33
+ tail -20 test_output.log
34
+
35
+ echo " "
36
+ echo " === Checking for Jest hanging indicators ==="
37
+ if grep -q " Jest did not exit one second after the test run has completed" test_output.log; then
38
+ echo " ❌ Found Jest hanging message"
39
+
40
+ echo " "
41
+ echo " === Common causes of Jest hanging ==="
42
+ echo " 1. Unclosed database connections"
43
+ echo " 2. Open timers/intervals"
44
+ echo " 3. Open file handles"
45
+ echo " 4. Unresolved promises"
46
+ echo " 5. WebSocket connections"
47
+ echo " 6. Event listeners not cleaned up"
48
+
49
+ echo " "
50
+ echo " === Analyzing test files for potential issues ==="
51
+
52
+ # Check for Redis connections
53
+ echo " Checking for Redis connection issues..."
54
+ grep -r " redisClient" src/ --include=" *.test.ts" | head -5
55
+
56
+ # Check for timers
57
+ echo " "
58
+ echo " Checking for setTimeout/setInterval..."
59
+ grep -r " setTimeout\|setInterval" src/ --include=" *.test.ts" | head -5
60
+
61
+ # Check for event listeners
62
+ echo " "
63
+ echo " Checking for event listeners..."
64
+ grep -r " addEventListener\|on(" src/ --include=" *.test.ts" | head -5
65
+
66
+ echo " "
67
+ echo " === Suggested fixes ==="
68
+ echo " 1. Add --detectOpenHandles to Jest config"
69
+ echo " 2. Ensure all Redis connections are closed in afterAll/afterEach"
70
+ echo " 3. Clear all timers in test cleanup"
71
+ echo " 4. Add explicit process.exit() if needed"
72
+
73
+ else
74
+ echo " ✅ No Jest hanging message found"
75
+ fi
76
+
77
+ echo " "
78
+ echo " === Running with --detectOpenHandles ==="
79
+ echo " This will show what's keeping Node.js alive..."
80
+ timeout 15s npm test -- --detectOpenHandles > detect_handles.log 2>&1 &
81
+ DETECT_PID=$!
82
+ wait $DETECT_PID
83
+ DETECT_EXIT=$?
84
+
85
+ if [ $DETECT_EXIT -eq 124 ]; then
86
+ echo " ❌ --detectOpenHandles also timed out"
87
+ else
88
+ echo " ✅ --detectOpenHandles completed"
89
+ fi
90
+
91
+ echo " "
92
+ echo " === Open handles output ==="
93
+ tail -30 detect_handles.log
94
+
95
+ echo " "
96
+ echo " === Cleanup ==="
97
+ rm -f test_output.log detect_handles.log
98
+ echo " Debug complete!"
0 commit comments