|
| 1 | +const PENDING_REQUESTS = 'Pending AJAX requests'; |
| 2 | +const PENDING_TEST_WAITERS = 'Pending test waiters: YES'; |
| 3 | +const PENDING_TIMERS = 'Pending timers'; |
| 4 | +const PENDING_SCHEDULED_ITEMS = 'Pending scheduled items'; |
| 5 | +const ACTIVE_RUNLOOPS = 'Active runloops: YES'; |
| 6 | + |
| 7 | +export function getSummary(testCounts, leakCounts, testNames) { |
| 8 | + let leakInfo = |
| 9 | + leakCounts.length > 0 |
| 10 | + ? `We found the following information that may help you identify code that violated test isolation: \n |
| 11 | + ${leakCounts.join('\n')} |
| 12 | + \n` |
| 13 | + : ''; |
| 14 | + let summary = `TESTS ARE NOT ISOLATED\n |
| 15 | + The following ${testCounts} test(s) have one or more issues that are resulting in non-isolation (async execution is extending beyond the duration of the test):\n |
| 16 | + ${testNames.join('\n')} |
| 17 | +\n |
| 18 | + ${leakInfo} |
| 19 | + More information has been printed to the console. Please use that information to help in debugging. |
| 20 | +`; |
| 21 | + |
| 22 | + return summary; |
| 23 | +} |
| 24 | + |
| 25 | +export default class TestDebugInfoSummary { |
| 26 | + constructor() { |
| 27 | + this._testDebugInfos = []; |
| 28 | + this.fullTestNames = []; |
| 29 | + this.hasPendingRequests = false; |
| 30 | + this.hasPendingWaiters = false; |
| 31 | + this.hasRunLoop = false; |
| 32 | + this.hasPendingTimers = false; |
| 33 | + this.hasPendingScheduledQueueItems = false; |
| 34 | + this.totalPendingRequestCount = 0; |
| 35 | + this.totalPendingTimersCount = 0; |
| 36 | + this.totalPendingScheduledQueueItemCount = 0; |
| 37 | + } |
| 38 | + |
| 39 | + add(testDebugInfo) { |
| 40 | + let summary = testDebugInfo.summary; |
| 41 | + |
| 42 | + this._testDebugInfos.push(testDebugInfo); |
| 43 | + |
| 44 | + this.fullTestNames.push(summary.fullTestName); |
| 45 | + if (summary.hasPendingRequests) { |
| 46 | + this.hasPendingRequests = true; |
| 47 | + } |
| 48 | + if (summary.hasPendingWaiters) { |
| 49 | + this.hasPendingWaiters = true; |
| 50 | + } |
| 51 | + if (summary.hasRunLoop) { |
| 52 | + this.hasRunLoop = true; |
| 53 | + } |
| 54 | + if (summary.hasPendingTimers) { |
| 55 | + this.hasPendingTimers = true; |
| 56 | + } |
| 57 | + if (summary.pendingScheduledQueueItemCount > 0) { |
| 58 | + this.hasPendingScheduledQueueItems = true; |
| 59 | + } |
| 60 | + |
| 61 | + this.totalPendingRequestCount += summary.pendingRequestCount; |
| 62 | + this.totalPendingTimersCount += summary.pendingTimersCount; |
| 63 | + this.totalPendingScheduledQueueItemCount += summary.pendingScheduledQueueItemCount; |
| 64 | + } |
| 65 | + |
| 66 | + get hasDebugInfo() { |
| 67 | + return this._testDebugInfos.length > 0; |
| 68 | + } |
| 69 | + |
| 70 | + printToConsole(_console = console) { |
| 71 | + _console.group('Tests not isolated'); |
| 72 | + |
| 73 | + this._testDebugInfos.forEach(testDebugInfo => { |
| 74 | + let summary = testDebugInfo.summary; |
| 75 | + |
| 76 | + _console.group(summary.fullTestName); |
| 77 | + |
| 78 | + if (summary.hasPendingRequests) { |
| 79 | + _console.log(this.formatCount(PENDING_REQUESTS, summary.pendingRequestCount)); |
| 80 | + } |
| 81 | + |
| 82 | + if (summary.hasPendingWaiters) { |
| 83 | + _console.log(PENDING_TEST_WAITERS); |
| 84 | + } |
| 85 | + |
| 86 | + if (summary.hasPendingTimers) { |
| 87 | + _console.group(this.formatCount(PENDING_TIMERS, summary.pendingTimersCount)); |
| 88 | + summary.pendingTimersStackTraces.forEach(stackTrace => { |
| 89 | + _console.log(stackTrace); |
| 90 | + }); |
| 91 | + _console.groupEnd(); |
| 92 | + } |
| 93 | + |
| 94 | + if (summary.hasPendingScheduledQueueItems) { |
| 95 | + _console.group( |
| 96 | + this.formatCount(PENDING_SCHEDULED_ITEMS, summary.pendingScheduledQueueItemCount) |
| 97 | + ); |
| 98 | + summary.pendingScheduledQueueItemStackTraces.forEach(stackTrace => { |
| 99 | + _console.log(stackTrace); |
| 100 | + }); |
| 101 | + _console.groupEnd(); |
| 102 | + } |
| 103 | + |
| 104 | + if (summary.hasRunLoop) { |
| 105 | + _console.log(ACTIVE_RUNLOOPS); |
| 106 | + } |
| 107 | + |
| 108 | + _console.groupEnd(); |
| 109 | + }); |
| 110 | + |
| 111 | + _console.groupEnd(); |
| 112 | + } |
| 113 | + |
| 114 | + formatForBrowser() { |
| 115 | + let leakCounts = []; |
| 116 | + |
| 117 | + if (this.hasPendingRequests) { |
| 118 | + leakCounts.push(this.formatCount(PENDING_REQUESTS, this.totalPendingRequestCount)); |
| 119 | + } |
| 120 | + |
| 121 | + if (this.hasPendingWaiters) { |
| 122 | + leakCounts.push(PENDING_TEST_WAITERS); |
| 123 | + } |
| 124 | + |
| 125 | + if (this.hasPendingTimers) { |
| 126 | + leakCounts.push(this.formatCount(PENDING_TIMERS, this.totalPendingTimersCount)); |
| 127 | + } |
| 128 | + |
| 129 | + if (this.hasPendingScheduledQueueItems) { |
| 130 | + leakCounts.push( |
| 131 | + this.formatCount(PENDING_SCHEDULED_ITEMS, this.totalPendingScheduledQueueItemCount) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + if (this.hasRunLoop) { |
| 136 | + leakCounts.push(ACTIVE_RUNLOOPS); |
| 137 | + } |
| 138 | + |
| 139 | + return getSummary(this._testDebugInfos.length, leakCounts, this.fullTestNames); |
| 140 | + } |
| 141 | + |
| 142 | + formatCount(title, count) { |
| 143 | + return `${title}: ${count}`; |
| 144 | + } |
| 145 | +} |
0 commit comments