Open History to see the last commit
1import assert from 'node:assert/strict'
2import { spawnSync } from 'node:child_process'
3import fs from 'node:fs'
4import os from 'node:os'
5import path from 'node:path'
6import test from 'node:test'
7import { fileURLToPath } from 'node:url'
8import { run } from './run.mjs'
9β
10const scriptPath = fileURLToPath(new URL('./run.mjs', import.meta.url))
11const measurePath = fileURLToPath(new URL('./measure.mjs', import.meta.url))
12β
13/** @param {string} filePath */
14function writeCurrent(filePath, gzipBytes = 100) {
15 fs.mkdirSync(path.dirname(filePath), { recursive: true })
16 fs.writeFileSync(
17 filePath,
18 JSON.stringify({
19 metrics: [
20 {
21 id: 'react-router.minimal',
22 gzipBytes,
23 initialGzipBytes: gzipBytes,
24 rawBytes: gzipBytes * 3,
25 brotliBytes: gzipBytes - 10,
26 },
27 ],
28 }),
29 )
30}
31β
32/** @param {import('node:test').TestContext} t */
33function fixture(t) {
34 const root = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-runner-'))
35 t.after(() => fs.rmSync(root, { recursive: true, force: true }))
36 /** @type {Array<{command: string, args: string[], options: import('./run.mjs').CommandOptions}>} */
37 const calls = []
38 /** @type {string[]} */
39 const messages = []
40 /** @type {string[]} */
41 const reports = []
42 t.mock.method(
43 console,
44 'error',
45 /** @param {string} message */
46 (message) => messages.push(message),
47 )
48β
49 /** @type {import('./run.mjs').Execute} */
50 const execute = (command, args, options) => {
51 calls.push({ command, args, options })
52 if (args[0] === measurePath) {
53 const outputDir = args[args.indexOf('--results-dir') + 1]
54 assert.ok(outputDir)
55 writeCurrent(path.join(outputDir, 'current.json'), 90)
56 }
57 if (options.stdio !== 'inherit') {
58 fs.writeSync(options.stdio[1], 'noisy stdout\n')
59 fs.writeSync(options.stdio[2], 'noisy stderr\n')
60 return { status: 0 }
61 }
62 const result = spawnSync(command, args, {
63 ...options,
64 stdio: 'pipe',
65 encoding: 'utf8',
66 })
67 reports.push(result.stdout)
68 return result
69 }
70β
71 return { root, calls, messages, reports, execute }
72}
73β
74test('runs targeted tests before measurement and saves named results and logs', (t) => {
75 const { root, calls, messages, reports, execute } = fixture(t)
76 const status = run(
77 [
78 '--results-dir',
79 root,
80 '--name',
81 'baseline',
82 '--scenario',
83 'react-router.minimal,react-router.full',
84 '--test-projects',
85 '@tanstack/router-core,@tanstack/react-router',
86 '--',
87 'tests/path.test.ts',
88 'tests/link.test.tsx',
89 '-t',
90 'links with spaces',
91 ],
92 execute,
93 )
94β
95 assert.equal(status, 0)
96 assert.equal(calls.length, 3)
97 const [tests, measure] = calls
98 assert.ok(tests && measure)
99 assert.ok(tests.args.includes('--target=test:unit'))
100 assert.ok(tests.args.includes('--parallel=1'))
101 assert.ok(tests.args.includes('--skipRemoteCache'))
102 assert.ok(
103 tests.args.includes(
104 '--projects=@tanstack/router-core,@tanstack/react-router',
105 ),
106 )
107 assert.deepEqual(tests.args.slice(-5), [
108 '--',
109 'tests/path.test.ts',
110 'tests/link.test.tsx',
111 '-t',
112 'links with spaces',
113 ])
114 assert.equal(measure.args[0], measurePath)
115 assert.ok(measure.args.includes('react-router.minimal,react-router.full'))
116 assert.ok(!measure.args.includes('--skip-package-builds'))
117 assert.ok(!measure.args.includes('tests/path.test.ts'))
118 for (const { options } of calls) {
119 assert.equal(options.env.CI, '1')
120 assert.equal(options.env.NX_DAEMON, 'false')
121 assert.ok(!('shell' in options))
122 }
123 const runDir = path.join(root, 'runs/baseline')
124 assert.ok(fs.existsSync(path.join(runDir, 'current.json')))
125 for (const step of ['tests', 'measure']) {
126 assert.equal(
127 fs.readFileSync(path.join(runDir, `${step}.log`), 'utf8'),
128 'noisy stdout\nnoisy stderr\n',
129 )
130 }
131 assert.ok(messages.every((message) => !message.includes('noisy')))
132 assert.match(reports.join(''), /react-router\.minimal gzip=90/)
133})
134β
135test('compares a named baseline against current results without changing the baseline', (t) => {
136 const { root, reports, execute } = fixture(t)
137 const baseline = path.join(root, 'runs/baseline/current.json')
138 writeCurrent(baseline)
139β
140 assert.equal(
141 run(['--results-dir', root, '--baseline', 'baseline'], execute),
142 0,
143 )
144 assert.match(
145 reports.join(''),
146 /react-router\.minimal 100 -> 90 \(-10\) initial=-10 raw=-30 brotli=-10/,
147 )
148 assert.equal(
149 JSON.parse(fs.readFileSync(baseline, 'utf8')).metrics[0].gzipBytes,
150 100,
151 )
152})
153β
154test('accepts an external baseline and forwards measurement options', (t) => {
155 const { root, calls, reports, execute } = fixture(t)
156 const baseline = path.join(root, 'external baseline.json')
157 writeCurrent(baseline)
158β
159 assert.equal(
160 run(
161 [
162 '--results-dir',
163 root,
164 '--baseline',
165 baseline,
166 '--dist-dir',
167 path.join(root, 'dist with spaces'),
168 '--analysis',
169 '--sourcemap',
170 '--skip-package-builds',
171 ],
172 execute,
173 ),
174 0,
175 )
176 assert.deepEqual(calls[0]?.args.slice(3), [
177 '--dist-dir',
178 path.join(root, 'dist with spaces'),
179 '--analysis',
180 '--sourcemap',
181 '--skip-package-builds',
182 ])
183 assert.match(reports.join(''), /100 -> 90 \(-10\)/)
184})
185β
186test('stops on test failure, preserves its exit code, and prints only a bounded log tail', (t) => {
187 const { root, messages } = fixture(t)
188 let callCount = 0
189 const log = `early output\n${'x'.repeat(10_000)}\n${'later output\n'.repeat(60)}last error\n`
190 const status = run(
191 ['--results-dir', root, '--test-projects', '@tanstack/react-router'],
192 (command, args, options) => {
193 callCount++
194 assert.ok(options.stdio !== 'inherit')
195 fs.writeSync(options.stdio[1], log)
196 return { status: 42 }
197 },
198 )
199β
200 assert.equal(status, 42)
201 assert.equal(callCount, 1)
202 assert.equal(fs.readFileSync(path.join(root, 'tests.log'), 'utf8'), log)
203 assert.ok(!fs.existsSync(path.join(root, 'measure.log')))
204 assert.match(messages.join('\n'), /tests failed: exit 42/)
205 assert.match(messages.join('\n'), /last error/)
206 assert.doesNotMatch(messages.join('\n'), /early output|xxx/)
207 const tail = messages.at(-1)
208 assert.ok(tail)
209 assert.ok(tail.split('\n').length <= 40)
210 assert.ok(tail.length <= 8000)
211})
212β
213test('bounds failure output even when a log contains one very long line', (t) => {
214 const { root, messages } = fixture(t)
215 assert.equal(
216 run(['--results-dir', root], (command, args, options) => {
217 assert.ok(options.stdio !== 'inherit')
218 fs.writeSync(options.stdio[1], 'x'.repeat(20_000))
219 return { status: 1 }
220 }),
221 1,
222 )
223 assert.equal(messages.at(-1), 'x'.repeat(8000))
224})
225β
226test('does not report stale results after a failed or interrupted measurement', async (t) => {
227 for (const result of [
228 { status: 17 },
229 { status: null, signal: 'SIGTERM' },
230 { status: null, error: new Error('spawn failed') },
231 ]) {
232 await t.test(
233 result.error?.message || result.signal || `exit ${result.status}`,
234 (t) => {
235 const { root, messages } = fixture(t)
236 let callCount = 0
237 writeCurrent(path.join(root, 'current.json'))
238 assert.equal(
239 run(['--results-dir', root], () => {
240 callCount++
241 return result
242 }),
243 result.status || 1,
244 )
245 assert.equal(callCount, 1)
246 assert.match(messages.join('\n'), /measure failed:/)
247 },
248 )
249 }
250})
251β
252test('rejects invalid inputs and protects saved results before running commands', (t) => {
253 const { root, calls, execute } = fixture(t)
254 const current = path.join(root, 'current.json')
255 writeCurrent(current)
256 writeCurrent(path.join(root, 'runs/baseline/current.json'))
257 fs.writeFileSync(path.join(root, 'empty.json'), '{"metrics":[]}')
258 fs.writeFileSync(path.join(root, 'invalid.json'), '{')
259 /** @type {Array<[string[], RegExp]>} */
260 const cases = [
261 [['--unknown'], /Unknown option/],
262 [['--', 'tests/path.test.ts'], /require --test-projects/],
263 [['--test-projects', ' , '], /at least one project/],
264 [['--scenario', ' , '], /at least one scenario/],
265 [['--name', '../escape'], /Run names/],
266 [['--name', 'baseline'], /Named run already exists/],
267 [['--baseline', 'missing'], /ENOENT/],
268 [['--baseline', current], /baseline cannot be the current output/],
269 [['--baseline', path.join(root, 'empty.json')], /no metrics/],
270 [['--baseline', path.join(root, 'invalid.json')], /JSON/],
271 ]
272 for (const [args, error] of cases) {
273 assert.throws(() => run(['--results-dir', root, ...args], execute), error)
274 }
275 assert.equal(calls.length, 0)
276 assert.equal(
277 JSON.parse(fs.readFileSync(current, 'utf8')).metrics[0].gzipBytes,
278 100,
279 )
280})
281β
282test('reports help and argument errors through the CLI entry point', () => {
283 const help = spawnSync(process.execPath, [scriptPath, '--help'], {
284 encoding: 'utf8',
285 })
286 assert.equal(help.status, 0)
287 assert.match(help.stdout, /Usage: pnpm benchmark:bundle-size:run/)
288 const invalid = spawnSync(process.execPath, [scriptPath, '--unknown'], {
289 encoding: 'utf8',
290 })
291 assert.equal(invalid.status, 1)
292 assert.match(invalid.stderr, /Unknown option/)
293})