Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

pr-report.test.mjs

164 lines4.8 KB

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