|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { execFile } from 'node:child_process' |
| 3 | +import { promises as fs } from 'node:fs' |
| 4 | +import os from 'node:os' |
| 5 | +import path from 'node:path' |
| 6 | +import { promisify } from 'node:util' |
| 7 | +import test from 'node:test' |
| 8 | + |
| 9 | +const execFileAsync = promisify(execFile) |
| 10 | +const reportScript = new URL('./pr-report.mjs', import.meta.url) |
| 11 | + |
| 12 | +function metric(id, gzipBytes) { |
| 13 | + return { |
| 14 | + id, |
| 15 | + gzipBytes, |
| 16 | + rawBytes: gzipBytes * 3, |
| 17 | + brotliBytes: gzipBytes - 100, |
| 18 | + initialGzipBytes: gzipBytes - 10, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +async function generateReport({ current, baseline, history, baseSha }) { |
| 23 | + const fixtureDir = await fs.mkdtemp( |
| 24 | + path.join(os.tmpdir(), 'bundle-size-pr-report-'), |
| 25 | + ) |
| 26 | + |
| 27 | + try { |
| 28 | + const currentPath = path.join(fixtureDir, 'current.json') |
| 29 | + const outputPath = path.join(fixtureDir, 'report.md') |
| 30 | + const args = [ |
| 31 | + reportScript.pathname, |
| 32 | + '--current', |
| 33 | + currentPath, |
| 34 | + '--output', |
| 35 | + outputPath, |
| 36 | + ] |
| 37 | + |
| 38 | + await fs.writeFile(currentPath, JSON.stringify(current)) |
| 39 | + |
| 40 | + if (baseline) { |
| 41 | + const baselinePath = path.join(fixtureDir, 'baseline.json') |
| 42 | + await fs.writeFile(baselinePath, JSON.stringify(baseline)) |
| 43 | + args.push('--baseline', baselinePath) |
| 44 | + } |
| 45 | + |
| 46 | + if (history) { |
| 47 | + const historyPath = path.join(fixtureDir, 'history.json') |
| 48 | + await fs.writeFile(historyPath, JSON.stringify(history)) |
| 49 | + args.push('--history', historyPath) |
| 50 | + } |
| 51 | + |
| 52 | + if (baseSha) { |
| 53 | + args.push('--base-sha', baseSha) |
| 54 | + } |
| 55 | + |
| 56 | + await execFileAsync(process.execPath, args) |
| 57 | + return await fs.readFile(outputPath, 'utf8') |
| 58 | + } finally { |
| 59 | + await fs.rm(fixtureDir, { recursive: true, force: true }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function currentJson(metrics) { |
| 64 | + return { |
| 65 | + benchmarkName: 'Bundle Size (gzip)', |
| 66 | + measuredAt: '2026-07-19T09:45:56.787Z', |
| 67 | + sha: '126aa13f296b1234', |
| 68 | + metrics, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +test('renders a concise message when no measured scenario changed', async () => { |
| 73 | + const current = currentJson([metric('react-router.minimal', 1_000)]) |
| 74 | + const report = await generateReport({ current, baseline: current }) |
| 75 | + |
| 76 | + assert.equal( |
| 77 | + report, |
| 78 | + '<!-- bundle-size-benchmark -->\n## Bundle Size Benchmarks\n\nThis pull request does not affect bundle size in any measured scenario.\n', |
| 79 | + ) |
| 80 | +}) |
| 81 | + |
| 82 | +test('renders only scenarios that changed against the historical baseline', async () => { |
| 83 | + const current = currentJson([ |
| 84 | + metric('react-router.minimal', 1_000), |
| 85 | + metric('react-router.full', 1_200), |
| 86 | + ]) |
| 87 | + const history = { |
| 88 | + entries: { |
| 89 | + [current.benchmarkName]: [ |
| 90 | + { |
| 91 | + commit: { id: 'baseline-sha' }, |
| 92 | + benches: [ |
| 93 | + { name: 'react-router.minimal', value: 1_000 }, |
| 94 | + { name: 'react-router.full', value: 1_100 }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + } |
| 100 | + const report = await generateReport({ |
| 101 | + current, |
| 102 | + history, |
| 103 | + baseSha: 'baseline-sha', |
| 104 | + }) |
| 105 | + |
| 106 | + assert.match( |
| 107 | + report, |
| 108 | + /The following scenarios have bundle-size changes compared with the baseline:/, |
| 109 | + ) |
| 110 | + assert.doesNotMatch(report, /`react-router\.minimal`/) |
| 111 | + assert.match( |
| 112 | + report, |
| 113 | + /\| `react-router\.full` \| 1\.17 KiB \| \+100 B \(\+9\.09%\) \|/, |
| 114 | + ) |
| 115 | +}) |
| 116 | + |
| 117 | +test('keeps scenarios that do not have baseline data visible', async () => { |
| 118 | + const current = currentJson([ |
| 119 | + metric('react-router.minimal', 1_000), |
| 120 | + metric('new-scenario', 900), |
| 121 | + ]) |
| 122 | + const baseline = currentJson([metric('react-router.minimal', 1_000)]) |
| 123 | + const report = await generateReport({ current, baseline }) |
| 124 | + |
| 125 | + assert.match( |
| 126 | + report, |
| 127 | + /The following scenarios have bundle-size changes or lack baseline data for comparison:/, |
| 128 | + ) |
| 129 | + assert.doesNotMatch(report, /`react-router\.minimal`/) |
| 130 | + assert.match(report, /\| `new-scenario` \| 900 B \| n\/a \|/) |
| 131 | +}) |
0 commit comments