|
| 1 | +// @flow |
| 2 | +import expect from 'jest-matchers'; |
| 3 | +import jestMock from 'jest-mock'; |
| 4 | +import { getCurrentManager } from '../../compile'; |
| 5 | + |
| 6 | +const describe = (name, fn) => { |
| 7 | + const testRunner = getCurrentManager().testRunner; |
| 8 | + testRunner.setCurrentDescribe(name); |
| 9 | + fn(); |
| 10 | + testRunner.resetCurrentDescribe(name); |
| 11 | +}; |
| 12 | + |
| 13 | +const test = (name, fn) => { |
| 14 | + const testRunner = getCurrentManager().testRunner; |
| 15 | + let error = false; |
| 16 | + try { |
| 17 | + fn(); |
| 18 | + } catch (Error) { |
| 19 | + error = true; |
| 20 | + testRunner.addResult({ status: 'fail', name }); |
| 21 | + } finally { |
| 22 | + if (!error) { |
| 23 | + testRunner.addResult({ status: 'pass', name }); |
| 24 | + } |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +export default class TestRunner { |
| 29 | + tests: Array; |
| 30 | + aggregatedResults: Object; |
| 31 | + currentDescribe: String; |
| 32 | + manager: Manager; |
| 33 | + startTime: Date; |
| 34 | + endTime: Date; |
| 35 | + |
| 36 | + constructor(manager) { |
| 37 | + this.tests = []; |
| 38 | + this.aggregatedResults = this._makeEmptyAggregatedResults(); |
| 39 | + this.currentDescribe = ''; |
| 40 | + this.currentPath = ''; |
| 41 | + this.manager = manager; |
| 42 | + this.startTime = Date.now(); |
| 43 | + this.endTime = Date.now(); |
| 44 | + } |
| 45 | + |
| 46 | + _makeEmptyAggregatedResults() { |
| 47 | + return { |
| 48 | + failedTestSuites: 0, |
| 49 | + failedTests: 0, |
| 50 | + passedTestSuites: 0, |
| 51 | + passedTests: 0, |
| 52 | + totalTestSuites: 0, |
| 53 | + totalTests: 0, |
| 54 | + summaryMessage: '', |
| 55 | + failedMessages: [], |
| 56 | + results: [], |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + initialize() { |
| 61 | + this.resetResults(); |
| 62 | + this.startTime = Date.now(); |
| 63 | + this.endTime = Date.now(); |
| 64 | + } |
| 65 | + |
| 66 | + static testGlobals() { |
| 67 | + return { |
| 68 | + describe, |
| 69 | + test, |
| 70 | + it: test, |
| 71 | + expect, |
| 72 | + jest: jestMock, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + findTests(modules) { |
| 77 | + this.tests = modules.filter(m => { |
| 78 | + let matched = false; |
| 79 | + if ( |
| 80 | + m.path.includes('__tests__') && |
| 81 | + (m.path.endsWith('.js') || m.path.endsWith('.ts')) |
| 82 | + ) { |
| 83 | + matched = true; |
| 84 | + } |
| 85 | + if (m.path.endsWith('.test.js') || m.path.endsWith('.test.ts')) { |
| 86 | + matched = true; |
| 87 | + } |
| 88 | + if (m.path.endsWith('.spec.js') || m.path.endsWith('.spec.ts')) { |
| 89 | + matched = true; |
| 90 | + } |
| 91 | + return matched; |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + async transpileTests() { |
| 96 | + for (let t of this.tests) { |
| 97 | + await this.manager.transpileModules(t); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + async runTests() { |
| 102 | + await this.transpileTests(); |
| 103 | + this.tests.forEach(t => { |
| 104 | + this.setCurrentPath(t.path); |
| 105 | + this.manager.evaluateModule(t); |
| 106 | + this.resetCurrentPath(); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + addResult({ status, name }) { |
| 111 | + let describe = this.currentDescribe; |
| 112 | + let path = this.currentPath; |
| 113 | + |
| 114 | + this.aggregatedResults.results.push({ status, name, describe, path }); |
| 115 | + |
| 116 | + this.aggregatedResults.totalTests++; |
| 117 | + if (status === 'pass') { |
| 118 | + this.aggregatedResults.passedTests++; |
| 119 | + } else { |
| 120 | + this.aggregatedResults.failedTests++; |
| 121 | + } |
| 122 | + let totalTestSuites = new Set(); |
| 123 | + let failedTestSuites = new Set(); |
| 124 | + this.aggregatedResults.results.forEach(({ status, path }) => { |
| 125 | + totalTestSuites.add(path); |
| 126 | + if (status === 'fail') { |
| 127 | + failedTestSuites.add(path); |
| 128 | + } |
| 129 | + }); |
| 130 | + this.aggregatedResults.totalTestSuites = totalTestSuites.size; |
| 131 | + this.aggregatedResults.failedTestSuites = failedTestSuites.size; |
| 132 | + this.aggregatedResults.passedTestSuites = |
| 133 | + totalTestSuites.size - failedTestSuites.size; |
| 134 | + } |
| 135 | + |
| 136 | + reportResults() { |
| 137 | + let aggregatedResults = this.aggregatedResults; |
| 138 | + let results = this.aggregatedResults.results; |
| 139 | + let summaryMessage = ''; |
| 140 | + let failedMessages = []; |
| 141 | + let summaryEmoji = ''; |
| 142 | + |
| 143 | + if (aggregatedResults.totalTestSuites === 0) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + results.forEach(({ status, name, describe, path }) => { |
| 148 | + if (status === 'fail') { |
| 149 | + let message = `FAIL (${path}) `; |
| 150 | + if (describe) { |
| 151 | + message += `${describe} > `; |
| 152 | + } |
| 153 | + message += `${name}`; |
| 154 | + failedMessages.push(message); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + summaryEmoji = |
| 159 | + aggregatedResults.totalTestSuites === aggregatedResults.passedTestSuites |
| 160 | + ? '😎' |
| 161 | + : '👻'; |
| 162 | + summaryMessage = `Test Summary: ${summaryEmoji}\n\n`; |
| 163 | + summaryMessage += 'Test Suites: '; |
| 164 | + if (aggregatedResults.failedTestSuites !== null) { |
| 165 | + summaryMessage += `${aggregatedResults.failedTestSuites} failed, `; |
| 166 | + } |
| 167 | + if (aggregatedResults.passedTestSuites !== null) { |
| 168 | + summaryMessage += `${aggregatedResults.passedTestSuites} passed, `; |
| 169 | + } |
| 170 | + summaryMessage += `${aggregatedResults.totalTestSuites} total`; |
| 171 | + summaryMessage += '\n'; |
| 172 | + |
| 173 | + summaryMessage += 'Tests: '; |
| 174 | + if (aggregatedResults.failedTests !== null) { |
| 175 | + summaryMessage += `${aggregatedResults.failedTests} failed, `; |
| 176 | + } |
| 177 | + if (aggregatedResults.passedTests !== null) { |
| 178 | + summaryMessage += `${aggregatedResults.passedTests} passed, `; |
| 179 | + } |
| 180 | + summaryMessage += `${aggregatedResults.totalTests} total`; |
| 181 | + summaryMessage += '\n'; |
| 182 | + |
| 183 | + this.endTime = Date.now(); |
| 184 | + this.duration = this.endTime - this.startTime; |
| 185 | + summaryMessage += `Time: ${this.duration}ms`; |
| 186 | + summaryMessage += '\n'; |
| 187 | + |
| 188 | + aggregatedResults.summaryMessage = summaryMessage; |
| 189 | + aggregatedResults.failedMessages = failedMessages; |
| 190 | + return aggregatedResults; |
| 191 | + } |
| 192 | + |
| 193 | + resetResults() { |
| 194 | + this.aggregatedResults = this._makeEmptyAggregatedResults(); |
| 195 | + } |
| 196 | + |
| 197 | + setCurrentDescribe(name) { |
| 198 | + if (this.currentDescribe) { |
| 199 | + this.currentDescribe += ` > ${name}`; |
| 200 | + } else { |
| 201 | + this.currentDescribe += `${name}`; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + resetCurrentDescribe() { |
| 206 | + this.currentDescribe = ''; |
| 207 | + } |
| 208 | + |
| 209 | + setCurrentPath(name) { |
| 210 | + this.currentPath = name; |
| 211 | + } |
| 212 | + |
| 213 | + resetCurrentPath() { |
| 214 | + this.currentPath = ''; |
| 215 | + } |
| 216 | +} |
0 commit comments