|
| 1 | +import {format, formatResult} from '.'; |
| 2 | + |
| 3 | +test('does nothing without arguments', () => { |
| 4 | + const actual = format(); |
| 5 | + expect(actual).toEqual(''); |
| 6 | +}); |
| 7 | + |
| 8 | +test('does nothing without report results', () => { |
| 9 | + const actual = format({results: []}); |
| 10 | + expect(actual).toEqual(''); |
| 11 | +}); |
| 12 | + |
| 13 | +test('does nothing without .errors and .warnings', () => { |
| 14 | + const actual = format({results: [{}]}); |
| 15 | + expect(actual).toEqual(''); |
| 16 | +}); |
| 17 | + |
| 18 | +test('returns empty summary if verbose', () => { |
| 19 | + const actual = format({ |
| 20 | + results: [ |
| 21 | + { |
| 22 | + errors: [], |
| 23 | + warnings: [] |
| 24 | + } |
| 25 | + ] |
| 26 | + }, { |
| 27 | + verbose: true |
| 28 | + }); |
| 29 | + |
| 30 | + expect(actual).toContain('0 problems, 0 warnings'); |
| 31 | +}); |
| 32 | + |
| 33 | +test('returns a correct summary of empty .errors and .warnings', () => { |
| 34 | + const actualError = format({ |
| 35 | + results: [ |
| 36 | + { |
| 37 | + errors: [ |
| 38 | + { |
| 39 | + level: 2, |
| 40 | + name: 'error-name', |
| 41 | + message: 'There was an error' |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + ] |
| 46 | + }); |
| 47 | + |
| 48 | + const actualWarning = format({ |
| 49 | + results: [ |
| 50 | + { |
| 51 | + warnings: [ |
| 52 | + { |
| 53 | + level: 1, |
| 54 | + name: 'warning-name', |
| 55 | + message: 'There was a problem' |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + ] |
| 60 | + }); |
| 61 | + |
| 62 | + expect(actualError).toContain('There was an error'); |
| 63 | + expect(actualError).toContain('1 problems, 0 warnings'); |
| 64 | + expect(actualWarning).toContain('There was a problem'); |
| 65 | + expect(actualWarning).toContain('0 problems, 1 warnings'); |
| 66 | +}); |
| 67 | + |
| 68 | +test('uses appropriate signs by default', () => { |
| 69 | + const actualError = format({ |
| 70 | + results: [ |
| 71 | + { |
| 72 | + errors: [ |
| 73 | + { |
| 74 | + level: 2, |
| 75 | + name: 'error-name', |
| 76 | + message: 'There was an error' |
| 77 | + } |
| 78 | + ] |
| 79 | + } |
| 80 | + ] |
| 81 | + }); |
| 82 | + |
| 83 | + const actualWarning = format({ |
| 84 | + results: [ |
| 85 | + { |
| 86 | + warnings: [ |
| 87 | + { |
| 88 | + level: 1, |
| 89 | + name: 'warning-name', |
| 90 | + message: 'There was a problem' |
| 91 | + } |
| 92 | + ] |
| 93 | + } |
| 94 | + ] |
| 95 | + }); |
| 96 | + |
| 97 | + expect(actualError).toContain('✖'); |
| 98 | + expect(actualWarning).toContain('⚠'); |
| 99 | +}); |
| 100 | + |
| 101 | +test('uses signs as configured', () => { |
| 102 | + const options = {signs: ['HNT', 'WRN', 'ERR'] as [string, string, string]}; |
| 103 | + const actualError = format( |
| 104 | + { |
| 105 | + results: [ |
| 106 | + { |
| 107 | + errors: [ |
| 108 | + { |
| 109 | + level: 2, |
| 110 | + name: 'error-name', |
| 111 | + message: 'There was an error' |
| 112 | + } |
| 113 | + ] |
| 114 | + } |
| 115 | + ] |
| 116 | + }, |
| 117 | + options |
| 118 | + ); |
| 119 | + |
| 120 | + const actualWarning = format( |
| 121 | + { |
| 122 | + results: [ |
| 123 | + { |
| 124 | + warnings: [ |
| 125 | + { |
| 126 | + level: 1, |
| 127 | + name: 'warning-name', |
| 128 | + message: 'There was a problem' |
| 129 | + } |
| 130 | + ] |
| 131 | + } |
| 132 | + ] |
| 133 | + }, |
| 134 | + options |
| 135 | + ); |
| 136 | + |
| 137 | + expect(actualError).toContain('ERR'); |
| 138 | + expect(actualWarning).toContain('WRN'); |
| 139 | +}); |
| 140 | + |
| 141 | +test('format result provides summary without arguments', () => { |
| 142 | + const actual = formatResult(); |
| 143 | + const actualText = actual.join('\n'); |
| 144 | + |
| 145 | + expect(actualText).toContain('0 problems, 0 warnings'); |
| 146 | +}); |
| 147 | + |
| 148 | +test('format result transforms error to text', () => { |
| 149 | + const actual = formatResult({ |
| 150 | + errors: [ |
| 151 | + { |
| 152 | + level: 2, |
| 153 | + name: 'error-name', |
| 154 | + message: 'There was an error' |
| 155 | + } |
| 156 | + ] |
| 157 | + }); |
| 158 | + |
| 159 | + const actualText = actual.join('\n'); |
| 160 | + |
| 161 | + expect(actualText).toContain('error-name'); |
| 162 | + expect(actualText).toContain('There was an error'); |
| 163 | + expect(actualText).toContain('1 problems, 0 warnings'); |
| 164 | +}); |
| 165 | + |
| 166 | +test('format result transforms warning to text', () => { |
| 167 | + const actual = formatResult({ |
| 168 | + warnings: [ |
| 169 | + { |
| 170 | + level: 1, |
| 171 | + name: 'warning-name', |
| 172 | + message: 'There was a warning' |
| 173 | + } |
| 174 | + ] |
| 175 | + }); |
| 176 | + |
| 177 | + const actualText = actual.join('\n'); |
| 178 | + |
| 179 | + expect(actualText).toContain('warning-name'); |
| 180 | + expect(actualText).toContain('There was a warning'); |
| 181 | + expect(actualText).toContain('0 problems, 1 warnings'); |
| 182 | +}); |
| 183 | + |
| 184 | +test('format result prints help for errors', () => { |
| 185 | + const actual = formatResult({ |
| 186 | + errors: [ |
| 187 | + { |
| 188 | + level: 2, |
| 189 | + name: 'error-name', |
| 190 | + message: 'There was an error' |
| 191 | + } |
| 192 | + ] |
| 193 | + }, { |
| 194 | + helpUrl: 'https://example.com' |
| 195 | + }); |
| 196 | + |
| 197 | + expect(actual).toEqual(expect.arrayContaining([ |
| 198 | + expect.stringContaining('Get help:') |
| 199 | + ])); |
| 200 | +}); |
| 201 | + |
| 202 | +test('format result prints help for warnings', () => { |
| 203 | + const actual = formatResult({ |
| 204 | + warnings: [ |
| 205 | + { |
| 206 | + level: 2, |
| 207 | + name: 'warning-name', |
| 208 | + message: 'There was a warning' |
| 209 | + } |
| 210 | + ] |
| 211 | + }, { |
| 212 | + helpUrl: 'https://example.com' |
| 213 | + }); |
| 214 | + |
| 215 | + expect(actual).toEqual(expect.arrayContaining([ |
| 216 | + expect.stringContaining('Get help:') |
| 217 | + ])); |
| 218 | +}); |
| 219 | + |
| 220 | +test('format result help cotains options.helpUrl', () => { |
| 221 | + const helpUrl = 'https://example.com'; |
| 222 | + |
| 223 | + const actual = formatResult({ |
| 224 | + warnings: [ |
| 225 | + { |
| 226 | + level: 2, |
| 227 | + name: 'warning-name', |
| 228 | + message: 'There was a warning' |
| 229 | + } |
| 230 | + ] |
| 231 | + }, { |
| 232 | + helpUrl |
| 233 | + }); |
| 234 | + |
| 235 | + expect(actual).toEqual(expect.arrayContaining([ |
| 236 | + expect.stringContaining(helpUrl) |
| 237 | + ])); |
| 238 | +}); |
| 239 | + |
| 240 | +test('format result omits help for empty problems', () => { |
| 241 | + const actual = formatResult({ |
| 242 | + warnings: [] |
| 243 | + }); |
| 244 | + |
| 245 | + expect(actual).not.toEqual(expect.arrayContaining([ |
| 246 | + expect.stringContaining('Get help:') |
| 247 | + ])); |
| 248 | +}); |
0 commit comments