|
| 1 | +// From https://github.com/robertbradleyux/jest-ci-spec-reporter/blob/main/src/jest-ci-spec-reporter.ts |
| 2 | +/* |
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2023 Robert Bradley |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +*/ |
| 25 | +'use strict' |
| 26 | +Object.defineProperty(exports, '__esModule', { value: true }) |
| 27 | +class JestCiSpecReporter { |
| 28 | + onRunStart({ numTotalTestSuites }) { |
| 29 | + console.log() |
| 30 | + console.log(`Found ${numTotalTestSuites} test suites.`) |
| 31 | + console.log() |
| 32 | + } |
| 33 | + onRunComplete(_, results) { |
| 34 | + const { |
| 35 | + numFailedTests, |
| 36 | + numPassedTests, |
| 37 | + numPendingTests, |
| 38 | + testResults, |
| 39 | + numTotalTests, |
| 40 | + startTime, |
| 41 | + } = results |
| 42 | + testResults.forEach(({ failureMessage }) => { |
| 43 | + if (failureMessage) { |
| 44 | + console.log(failureMessage) |
| 45 | + } |
| 46 | + }) |
| 47 | + const testResultText = numFailedTests === 0 ? 'SUCCESS' : 'FAILED' |
| 48 | + const numNotSkippedTests = numPassedTests + numFailedTests |
| 49 | + const runDuration = this._getRunDuration(startTime) |
| 50 | + console.log() |
| 51 | + console.log( |
| 52 | + `Executed ${numNotSkippedTests} of ${numTotalTests} (skipped ${numPendingTests}) ${testResultText} (${runDuration})`, |
| 53 | + ) |
| 54 | + console.log(`TOTAL: ${numNotSkippedTests} ${testResultText}`) |
| 55 | + } |
| 56 | + onTestResult(test, { testResults }) { |
| 57 | + testResults.forEach((result) => { |
| 58 | + var _a, _b |
| 59 | + const { title, duration, status, ancestorTitles } = result |
| 60 | + const { name } = |
| 61 | + (_b = (_a = test.context.config) === null || _a === void 0 ? void 0 : _a.displayName) !== |
| 62 | + null && _b !== void 0 |
| 63 | + ? _b |
| 64 | + : {} |
| 65 | + if (name) { |
| 66 | + ancestorTitles.unshift(name) |
| 67 | + } |
| 68 | + const breadcrumbs = `${ancestorTitles.join(' > ')} >` |
| 69 | + |
| 70 | + console.log( |
| 71 | + ` ${this._getTestStatus(status)} ${breadcrumbs} ${title} ${this._getTestDuration(duration)}`, |
| 72 | + ) |
| 73 | + }) |
| 74 | + } |
| 75 | + getLastError() { |
| 76 | + return undefined |
| 77 | + } |
| 78 | + _getRunDuration(startTime) { |
| 79 | + const deltaInMillis = new Date().getTime() - new Date(startTime).getTime() |
| 80 | + const seconds = ((deltaInMillis % 60000) / 1000).toFixed(3) |
| 81 | + return `${seconds} secs` |
| 82 | + } |
| 83 | + _getTestDuration(duration) { |
| 84 | + return `\x1b[1m\x1b[30m(${duration !== null && duration !== void 0 ? duration : 0}ms)\x1b[0m` |
| 85 | + } |
| 86 | + _getTestStatus(status) { |
| 87 | + switch (status) { |
| 88 | + case 'passed': |
| 89 | + return '\x1b[1m\x1b[32m[PASS]\x1b[0m' |
| 90 | + case 'pending': |
| 91 | + return '\x1b[1m\x1b[33m[SKIP]\x1b[0m' |
| 92 | + case 'todo': |
| 93 | + return '\x1b[1m\x1b[34m[TODO]\x1b[0m' |
| 94 | + case 'failed': |
| 95 | + default: |
| 96 | + return '\x1b[1m\x1b[31m[FAIL]\x1b[0m' |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +exports.default = JestCiSpecReporter |
0 commit comments