|
| 1 | +import Handlebars from 'handlebars' |
| 2 | +import { registerAllHelpers } from '../../src/handlebars/helpers' |
| 3 | +import * as fs from 'fs' |
| 4 | +import * as path from 'path' |
| 5 | + |
| 6 | +// Register project's own helpers (string, array, math, ansi helpers) |
| 7 | +beforeAll(() => { |
| 8 | + registerAllHelpers() |
| 9 | + |
| 10 | + // Register real implementations of helpers that handlebars-helpers-ctrf |
| 11 | + // would normally provide (the package is mocked to no-op in Jest config) |
| 12 | + Handlebars.registerHelper('gt', (a: number, b: number) => a > b) |
| 13 | + Handlebars.registerHelper('lt', (a: number, b: number) => a < b) |
| 14 | + Handlebars.registerHelper('abs', (a: number) => Math.abs(a)) |
| 15 | + Handlebars.registerHelper( |
| 16 | + 'toPercent', |
| 17 | + (value: number, decimals: number = 2) => (value * 100).toFixed(decimals) |
| 18 | + ) |
| 19 | + Handlebars.registerHelper('formatDuration', (d: number) => `${d}ms`) |
| 20 | + Handlebars.registerHelper( |
| 21 | + 'formatDurationFromTimes', |
| 22 | + (start: number, stop: number) => `${stop - start}ms` |
| 23 | + ) |
| 24 | + Handlebars.registerHelper('addAll', (...args: unknown[]) => { |
| 25 | + const values = args.slice(0, -1) as number[] |
| 26 | + return values.reduce((sum, v) => sum + (v ?? 0), 0) |
| 27 | + }) |
| 28 | + Handlebars.registerHelper('add', (a: number, b: number) => a + b) |
| 29 | + Handlebars.registerHelper( |
| 30 | + 'countFlakyTests', |
| 31 | + (tests: Array<{ flaky?: boolean }>) => |
| 32 | + tests?.filter((t) => t.flaky).length ?? 0 |
| 33 | + ) |
| 34 | + Handlebars.registerHelper( |
| 35 | + 'sortTestsByFailRate', |
| 36 | + (tests: Array<Record<string, unknown>>) => tests ?? [] |
| 37 | + ) |
| 38 | + Handlebars.registerHelper( |
| 39 | + 'sortTestsByFlakyRate', |
| 40 | + (tests: Array<Record<string, unknown>>) => tests ?? [] |
| 41 | + ) |
| 42 | + Handlebars.registerHelper('getCtrfEmoji', () => '') |
| 43 | +}) |
| 44 | + |
| 45 | +function compileTemplate( |
| 46 | + templatePath: string, |
| 47 | + context: Record<string, unknown> |
| 48 | +): string { |
| 49 | + const source = fs.readFileSync( |
| 50 | + path.resolve(__dirname, '../../src/reports', templatePath), |
| 51 | + 'utf-8' |
| 52 | + ) |
| 53 | + const template = Handlebars.compile(source, { preventIndent: true }) |
| 54 | + return template(context) |
| 55 | +} |
| 56 | + |
| 57 | +describe('summary-delta-table.hbs delta display', () => { |
| 58 | + function makeContext(change: number) { |
| 59 | + return { |
| 60 | + ctrf: { |
| 61 | + summary: { |
| 62 | + tests: 10, |
| 63 | + passed: 8, |
| 64 | + failed: 1, |
| 65 | + skipped: 1, |
| 66 | + pending: 0, |
| 67 | + other: 0, |
| 68 | + start: 0, |
| 69 | + stop: 1000 |
| 70 | + }, |
| 71 | + tests: [] |
| 72 | + }, |
| 73 | + report: { |
| 74 | + results: { |
| 75 | + summary: { |
| 76 | + tests: 10, |
| 77 | + passed: 8, |
| 78 | + failed: 1, |
| 79 | + skipped: 1, |
| 80 | + pending: 0, |
| 81 | + other: 0, |
| 82 | + start: 0, |
| 83 | + stop: 1000 |
| 84 | + } |
| 85 | + }, |
| 86 | + insights: { |
| 87 | + extra: { |
| 88 | + summary: { |
| 89 | + tests: { current: 10, baseline: 10 - change, change }, |
| 90 | + passed: { current: 8, baseline: 8 - change, change }, |
| 91 | + failed: { current: 1, baseline: 1, change: 0 }, |
| 92 | + skipped: { current: 1, baseline: 1, change: 0 }, |
| 93 | + other: { current: 0, baseline: 0, change: 0 }, |
| 94 | + flaky: { current: 0, baseline: 0, change: 0 }, |
| 95 | + duration: { current: 1000, baseline: 1000, change: 0 } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + it('should show ↑ for positive change', () => { |
| 104 | + const result = compileTemplate('summary-delta-table.hbs', makeContext(3)) |
| 105 | + expect(result).toContain('↑3') |
| 106 | + }) |
| 107 | + |
| 108 | + it('should show ↓ for negative change', () => { |
| 109 | + const result = compileTemplate('summary-delta-table.hbs', makeContext(-2)) |
| 110 | + expect(result).toContain('↓2') |
| 111 | + }) |
| 112 | + |
| 113 | + it('should not show ±0 for zero change', () => { |
| 114 | + const result = compileTemplate('summary-delta-table.hbs', makeContext(0)) |
| 115 | + expect(result).not.toContain('±0') |
| 116 | + expect(result).not.toContain('↑') |
| 117 | + expect(result).not.toContain('↓') |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +describe('github.hbs delta display', () => { |
| 122 | + function makeContext(change: number) { |
| 123 | + return { |
| 124 | + ctrf: { |
| 125 | + summary: { |
| 126 | + tests: 10, |
| 127 | + passed: 8, |
| 128 | + failed: 1, |
| 129 | + skipped: 1, |
| 130 | + pending: 0, |
| 131 | + other: 0, |
| 132 | + start: 0, |
| 133 | + stop: 1000 |
| 134 | + }, |
| 135 | + tests: [] |
| 136 | + }, |
| 137 | + report: { |
| 138 | + insights: { |
| 139 | + extra: { |
| 140 | + summary: { |
| 141 | + failed: { change }, |
| 142 | + passed: { change }, |
| 143 | + skipped: { change: 0 }, |
| 144 | + pending: { change: 0 }, |
| 145 | + other: { change: 0 }, |
| 146 | + flaky: { change: 0 }, |
| 147 | + tests: { change }, |
| 148 | + duration: { change: 0 } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }, |
| 153 | + github: {} |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + it('should show ↑ for positive change', () => { |
| 158 | + const result = compileTemplate('github.hbs', makeContext(3)) |
| 159 | + expect(result).toContain('↑3') |
| 160 | + }) |
| 161 | + |
| 162 | + it('should show ↓ for negative change', () => { |
| 163 | + const result = compileTemplate('github.hbs', makeContext(-2)) |
| 164 | + expect(result).toContain('↓2') |
| 165 | + }) |
| 166 | + |
| 167 | + it('should not show ±0 for zero change', () => { |
| 168 | + const result = compileTemplate('github.hbs', makeContext(0)) |
| 169 | + expect(result).not.toContain('±0') |
| 170 | + }) |
| 171 | +}) |
| 172 | + |
| 173 | +describe('fail-rate-table.hbs delta display', () => { |
| 174 | + function makeContext(change: number) { |
| 175 | + return { |
| 176 | + ctrf: { |
| 177 | + tests: [ |
| 178 | + { |
| 179 | + name: 'test1', |
| 180 | + status: 'passed', |
| 181 | + insights: { |
| 182 | + failRate: { current: 0.1, change }, |
| 183 | + extra: { |
| 184 | + totalResults: 10, |
| 185 | + totalResultsPassed: 9, |
| 186 | + totalResultsFailed: 1 |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + ] |
| 191 | + }, |
| 192 | + report: { |
| 193 | + insights: { |
| 194 | + failRate: { current: 0.1, change } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + it('should show ↑ for positive fail rate change', () => { |
| 201 | + const result = compileTemplate('fail-rate-table.hbs', makeContext(0.05)) |
| 202 | + expect(result).toContain('↑') |
| 203 | + }) |
| 204 | + |
| 205 | + it('should show ↓ for negative fail rate change', () => { |
| 206 | + const result = compileTemplate('fail-rate-table.hbs', makeContext(-0.05)) |
| 207 | + expect(result).toContain('↓') |
| 208 | + }) |
| 209 | + |
| 210 | + it('should not show ±0 for zero fail rate change', () => { |
| 211 | + const result = compileTemplate('fail-rate-table.hbs', makeContext(0)) |
| 212 | + expect(result).not.toContain('±0') |
| 213 | + }) |
| 214 | +}) |
| 215 | + |
| 216 | +describe('flaky-rate-table.hbs delta display', () => { |
| 217 | + function makeContext(change: number) { |
| 218 | + return { |
| 219 | + ctrf: { |
| 220 | + tests: [ |
| 221 | + { |
| 222 | + name: 'test1', |
| 223 | + status: 'passed', |
| 224 | + insights: { |
| 225 | + flakyRate: { current: 0.1, change }, |
| 226 | + extra: { totalResults: 10, totalAttemptsFlaky: 1 } |
| 227 | + } |
| 228 | + } |
| 229 | + ] |
| 230 | + }, |
| 231 | + report: { |
| 232 | + insights: { |
| 233 | + flakyRate: { current: 0.1, change } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + it('should show ↑ for positive flaky rate change', () => { |
| 240 | + const result = compileTemplate('flaky-rate-table.hbs', makeContext(0.05)) |
| 241 | + expect(result).toContain('↑') |
| 242 | + }) |
| 243 | + |
| 244 | + it('should show ↓ for negative flaky rate change', () => { |
| 245 | + const result = compileTemplate('flaky-rate-table.hbs', makeContext(-0.05)) |
| 246 | + expect(result).toContain('↓') |
| 247 | + }) |
| 248 | + |
| 249 | + it('should not show ±0 for zero flaky rate change', () => { |
| 250 | + const result = compileTemplate('flaky-rate-table.hbs', makeContext(0)) |
| 251 | + expect(result).not.toContain('±0') |
| 252 | + }) |
| 253 | +}) |
0 commit comments