-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
run.test.js
393 lines (325 loc) · 10.7 KB
/
run.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { join } from 'node:path'
import { expect, it, vi } from 'vitest'
import run from '../run'
vi.mock('../../time/get-running-time', () => ({
getRunningTime: () => 1
}))
vi.mock('../../time/cache', () => ({
getCache() {
return false
},
saveCache() {}
}))
vi.mock('nanospinner', () => {
return {
createSpinner() {
return {
error() {},
start() {
return this
},
success() {}
}
}
}
})
const TMP_DIR = /size-limit-[\w-]+\/?/g
const ROOT = join(__dirname, '..', '..', '..')
const NODE_VERSION = parseInt(process.version.slice(1))
function fixture(...files) {
return join(ROOT, 'fixtures', ...files)
}
function createProcess(cwd, args = []) {
let history = {
exitCode: 0,
stderr: '',
stdout: ''
}
let process = {
argv: ['node', 'size-limit', ...args],
cwd() {
if (cwd.includes('/')) {
return cwd
} else {
return fixture(...(Array.isArray(cwd) ? cwd : [cwd]))
}
},
exit(code) {
history.exitCode = code
},
stderr: {
write(str) {
history.stderr += str.split(ROOT).join('').replace(TMP_DIR, '')
}
},
stdout: {
write(str) {
history.stdout += str.split(ROOT).join('').replace(TMP_DIR, '')
}
}
}
return [process, history]
}
function clean(output) {
return output
.replace(/\d+\.\d+\.\d+/g, '0.0.0')
.replace(/var\/folders\/(.*)\//g, 'tmp/')
.replace(/"cwd": "[^"]+"/, '"cwd": "/tmp/"')
.replace(/"webpackOutput": "[^"]+"/, '"webpackOutput": "/tmp/"')
}
async function check(cwd, args) {
let [process, history] = createProcess(cwd, args)
await run(process)
expect(history.stderr).toBe('')
expect(history.exitCode).toBe(0)
return history.stdout
}
async function error(cwd, args) {
let [process, history] = createProcess(cwd, args)
await run(process)
expect(history.stdout).toBe('')
expect(history.exitCode).toBe(1)
return history.stderr
}
async function checkJson(cwd, json) {
expect(clean(await check(cwd, ['--json']))).toEqual(
JSON.stringify(json, null, ' ') + '\n'
)
}
it('shows version', async () => {
let out = await check('file', ['--version'])
expect(out).toMatch(/^size-limit \d+.\d+.\d+\n$/m)
})
it('shows help', async () => {
expect(await check('file', ['--help'])).toMatchSnapshot()
})
it('shows webpack-related help', async () => {
expect(await check('webpack', ['--help'])).toMatchSnapshot()
})
it('uses dependencies to detect plugins', async () => {
expect(await check('dependencies', ['--help'])).toMatchSnapshot()
})
it('shows error on missed package.json', async () => {
expect(await error('/')).toMatchSnapshot()
})
it('shows syntax errors in package.json', async () => {
let stderr = await error('package-syntax')
expect(stderr).toContain('ERROR')
expect(stderr).toContain('SyntaxError')
})
it('shows error in JSON format', async () => {
let [process, history] = createProcess('/', ['--json'])
await run(process)
expect(history.exitCode).toBe(1)
expect(history.stderr).toBe('')
let output = JSON.parse(history.stdout)
expect(Object.keys(output)).toEqual(['error'])
expect(output.error).toContain('SizeLimitError: Size Limit didn’t find')
})
it('shows migration guide for npm users', async () => {
expect(await error('legacy-npm')).toMatchSnapshot()
})
it('shows migration guide for yarn users', async () => {
expect(await error('legacy-yarn')).toMatchSnapshot()
})
it('shows migration guide for pnpm users', async () => {
expect(await error('legacy-pnpm')).toMatchSnapshot()
})
it('shows migration guide for npm users: config 1, dep 0', async () => {
let [process, history] = createProcess('npm-with-config-without-dev')
await run(process)
expect(history.exitCode).toBe(1)
expect(history.stderr).toMatchSnapshot()
})
it('shows size-limit dependency warning', async () => {
let [process, history] = createProcess('warn')
await run(process)
expect(history.exitCode).toBe(0)
expect(history.stderr).toMatchSnapshot()
})
it('throws on unknown argument', async () => {
expect(await error('file', ['--unknown'])).toMatchSnapshot()
})
it('throws on --save-bundle argument without webpack', async () => {
expect(await error('file', ['--save-bundle'])).toMatchSnapshot()
})
it('throws on --save-bundle argument without DIR parameter', async () => {
expect(await error('webpack', ['--save-bundle'])).toMatchSnapshot()
expect(
await error('webpack', ['--save-bundle', '--clean-dir'])
).toMatchSnapshot()
})
it('throws on --clean-dir argument without --save-bundle', async () => {
expect(await error('file', ['--clean-dir'])).toMatchSnapshot()
})
it('throws on --why argument without webpack', async () => {
expect(await error('file', ['--why'])).toMatchSnapshot()
})
it('throws on --why argument with esbuild but without esbuild-why', async () => {
expect(await error('esbuild-why-missing', ['--why'])).toMatchSnapshot()
})
it('throws on --compare-with argument without webpack', async () => {
expect(await error('file', ['--compare-with'])).toMatchSnapshot()
})
it('throws on --compare-with argument without --why', async () => {
expect(await error('webpack', ['--compare-with'])).toMatchSnapshot()
})
it('throws on --compare-with argument without value', async () => {
expect(await error('webpack', ['--why', '--compare-with'])).toMatchSnapshot()
})
it('throws on no config', async () => {
expect(await error('file')).toMatchSnapshot()
})
it('throws on non-array config', async () => {
expect(await error('non-array')).toMatchSnapshot()
})
it('throws on empty config', async () => {
expect(await error('empty')).toMatchSnapshot()
})
it('throws on non-object check', async () => {
expect(await error('non-object')).toMatchSnapshot()
})
it('throws on non-string path', async () => {
expect(await error('non-string')).toMatchSnapshot()
})
it('throws on non-string entry', async () => {
expect(await error('non-string-entry')).toMatchSnapshot()
})
it('throws on webpack option without webpack plugin', async () => {
expect(await error('non-webpack')).toMatchSnapshot()
expect(await error('non-webpack-ignore')).toMatchSnapshot()
expect(await error('non-webpack-config')).toMatchSnapshot()
})
it('throws on brotli option without brotli plugin', async () => {
expect(await error('non-brotli')).toMatchSnapshot()
})
it('throws on running option without time plugin', async () => {
expect(await error('non-time')).toMatchSnapshot()
})
it('throws on time limit without time plugin', async () => {
expect(await error('simple', ['--limit', '1 s'])).toMatchSnapshot()
})
it('throws on unknown option', async () => {
expect(await error('unknown')).toMatchSnapshot()
})
it('supports path without dot', async () => {
expect(await check('relative')).toMatchSnapshot()
})
it('works in integration test with JSON', async () => {
expect(await check('integration', ['--json'])).toMatchSnapshot()
})
it('shows gzip text when only gzip in config', async () => {
expect(await check('gzip')).toMatchSnapshot()
})
it('shows gzip text when brotli and gzip in config', async () => {
expect(await check('gzip-with-brotli')).toMatchSnapshot()
})
it('works in integration test with size', async () => {
expect(await check('integration')).toMatchSnapshot()
})
it('works in integration test with ESM', async () => {
await checkJson('integration-esm', [
{
name: 'index.js',
passed: true,
size: 1,
sizeLimit: 1
},
{
name: 'all',
passed: true,
size: 48,
sizeLimit: 48
}
])
})
it('works in integration test with time', async () => {
expect(await check('integration', ['--limit', '2s'])).toMatchSnapshot()
})
it('shows error when file not found for simple case', async () => {
let [process, history] = createProcess('file-not-found')
await run(process)
expect(history.exitCode).toBe(1)
expect(history.stderr).toBe('')
expect(history.stdout).toMatchSnapshot()
})
it('shows error when file not found for webpack', async () => {
let [process, history] = createProcess('webpack-no-files')
await run(process)
expect(history.exitCode).toBe(1)
expect(history.stderr).toBe('')
expect(history.stdout).toMatchSnapshot()
})
it('shows error when using brotli without file', async () => {
expect(await error('brotli-without-file')).toMatchSnapshot()
})
it('shows error on time bigger than limit', async () => {
let [process, history] = createProcess('integration', ['--limit', '1 s'])
await run(process)
expect(history.exitCode).toBe(1)
expect(history.stderr).toBe('')
expect(history.stdout).toMatchSnapshot()
})
it('shows error on time bigger than limit. in silent mode', async () => {
let [process, history] = createProcess('integration', [
'--silent',
'--limit',
'1 s'
])
await run(process)
expect(history.exitCode).toBe(1)
expect(history.stderr).toBe('')
expect(history.stdout).toMatchSnapshot()
})
it('shows nothing in silent mode and success check', async () => {
expect(await check('integration', ['--silent'])).toMatchSnapshot()
})
it('returns zero for empty file with webpack', async () => {
expect(await check('zero-webpack')).toMatchSnapshot()
})
it('returns zero for empty webpack file without compression', async () => {
expect(await check('zero-webpack-non-compression')).toMatchSnapshot()
})
it('returns zero for empty file with esbuild', async () => {
expect(await check('zero-esbuild')).toMatchSnapshot()
})
it('returns zero for empty esbuild file and with gzip', async () => {
expect(await check('zero-esbuild-gzip')).toMatchSnapshot()
})
it('returns zero for empty esbuild file and without compression', async () => {
expect(await check('zero-esbuild-non-compression')).toMatchSnapshot()
})
it.skipIf(NODE_VERSION < 21)(
'allows to use peer dependencies in import',
async () => {
await checkJson('combine', [
{ name: 'all', size: 2050 },
{ name: 'a', size: 1 },
{ name: 'redux', size: 2046 }
])
}
)
it('supports import and ignore for esbuild', async () => {
expect(clean(await check('peer-esbuild-non-compression'))).toMatchSnapshot()
})
it.skipIf(NODE_VERSION < 21)(
'supports import and ignore for esbuild and brotli',
async () => {
expect(clean(await check('peer-esbuild'))).toMatchSnapshot()
}
)
it.skipIf(NODE_VERSION < 21)(
'supports import and ignore for esbuild and gzip',
async () => {
expect(clean(await check('peer-esbuild-gzip'))).toMatchSnapshot()
}
)
it('shows debug', async () => {
expect(clean(await check('integration', ['--debug']))).toMatchSnapshot()
})
it('shows debug on error', async () => {
expect(clean(await error('internal-error', ['--debug']))).toMatchSnapshot()
})
it('find plugins in parent package.json', async () => {
expect(clean(await check(['nested', 'package']))).toMatchSnapshot()
})