/
cli.test.js
358 lines (320 loc) · 9.8 KB
/
cli.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
const spawn = require('cross-spawn')
const path = require('path')
function fixture (file) {
return path.join(__dirname, 'fixtures', file)
}
function run (args, options, env) {
if (!options) options = { }
options.env = {
TRAVIS_JOB_NUMBER: '1.1',
APPVEYOR_JOB_NUMBER: '1'
}
for (const i in process.env) {
if (!options.env[i]) options.env[i] = process.env[i]
}
for (const i in env) {
options.env[i] = env[i]
}
const cli = spawn(path.join(__dirname, '../cli.js'), args, options)
return new Promise(resolve => {
let out = ''
cli.stdout.on('data', data => {
out += data.toString()
})
cli.stderr.on('data', data => {
out += data.toString()
})
cli.on('close', code => {
resolve({ code, out })
})
})
}
function configError (msg) {
return ' ERROR ' + msg + '\n' +
' Fix it according to Size Limit docs.\n' +
'\n' +
' "size-limit": ['
}
it('returns help', () => {
return run(['--help']).then(result => {
expect(result.out).toContain('Options:')
expect(result.code).toEqual(0)
})
})
it('returns version', () => {
return run(['--version']).then(result => {
expect(result.out).toMatch(/\d+\.\d+\.\d+/)
expect(result.code).toEqual(0)
})
})
it('shows resolve errors', () => {
return run([], { cwd: fixture('unknown') }).then(result => {
expect(result.out).toContain('unknown.js')
expect(result.out).toMatch(/Size Limit can't resolve/)
expect(result.code).toEqual(1)
})
})
it('shows size-limit section error', () => {
return run([], { cwd: fixture('missed') }).then(result => {
expect(result.out).toContain(
' ERROR Can not find settings for Size Limit.\n' +
' Add it to section "size-limit" in package.json ' +
'according to Size Limit docs.\n' +
'\n' +
' "size-limit": [')
expect(result.code).toEqual(1)
})
})
it('shows size-limit type error', () => {
return run([], { cwd: fixture('type') }).then(result => {
expect(result.out).toContain(configError(
'The "size-limit" section of package.json must be an array.'
))
expect(result.code).toEqual(1)
})
})
it('shows size-limit section content error', () => {
return run([], { cwd: fixture('wrong-package') }).then(result => {
expect(result.out).toContain(configError(
'The path in Size Limit config must be a string or an array of strings.'
))
expect(result.code).toEqual(1)
})
})
it('shows config content error', () => {
return run([], { cwd: fixture('wrong-config') }).then(result => {
expect(result.out).toContain(
' ERROR Size Limit config must not be empty.\n' +
' Fix it according to Size Limit docs.\n' +
'\n' +
' [\n'
)
expect(result.code).toEqual(1)
})
})
it('uses .size-limit file config', () => {
return run([], { cwd: fixture('config') }).then(result => {
expect(result.out).toEqual('\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('uses .size-limit.js file config', () => {
return run([], { cwd: fixture('js') }).then(result => {
expect(result.out).toEqual('\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('overrides config by limit argument', () => {
return run(['--limit', '1B'], { cwd: fixture('config') }).then(result => {
expect(result.out).toContain('Size limit: 1 B\n')
expect(result.code).toEqual(3)
})
})
it('shows .size-limit error', () => {
return run([], { cwd: fixture('bad-config') }).then(result => {
expect(result.out).toContain(
' ERROR Can not parse .size-limit at 3:4.\n' +
' Missed comma between flow collection entries.'
)
expect(result.code).toEqual(1)
})
})
it('shows package.json error', () => {
return run([], { cwd: fixture('bad-package') }).then(result => {
expect(result.out).toContain(
' ERROR Can not parse package.json.\n' +
' Unexpected string'
)
expect(result.code).toEqual(1)
})
})
it('runs only on first job in Travis CI', () => {
const env = { TRAVIS: '1', TRAVIS_JOB_NUMBER: '1.2' }
return run([], { }, env).then(result => {
expect(result.out).toContain('first CI job')
expect(result.code).toEqual(0)
})
})
it('shows size without limit', () => {
return run([], { cwd: fixture('unlimit') }).then(result => {
expect(result.out).toEqual('\n' +
' Package size: 0 B\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('shows limit', () => {
return run([], { cwd: fixture('good') }).then(result => {
expect(result.out).toEqual('\n' +
' index.js\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
'\n' +
' index2.js\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
'\n' +
' index3.js\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
'\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('accepts array for path', () => {
return run([], { cwd: fixture('array') }).then(result => {
expect(result.out).toEqual('\n' +
' index1.js\n' +
' Package size: 13 B\n' +
'\n' +
' index1.js, index2.js\n' +
' Package size: 22 B\n' +
'\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('supports glob patterns', () => {
return run([], { cwd: fixture('glob') }).then(result => {
expect(result.out).toContain('Package size: 9 B\n')
expect(result.code).toEqual(0)
})
})
it('supports ES2016', () => {
return run([], { cwd: fixture('es2016') }).then(result => {
expect(result.out).toContain('Package size: 24 B\n')
expect(result.code).toEqual(0)
})
})
it('supports multiple files', () => {
return run([], { cwd: fixture('multiple') }).then(result => {
expect(result.out).toContain('Package size: 15 B\n')
expect(result.code).toEqual(0)
})
})
it('checks limits', () => {
return run([], { cwd: fixture('bad') }).then(result => {
expect(result.out).toContain('exceeded by 357 B')
expect(result.code).toEqual(3)
})
})
it('uses names', () => {
return run([], { cwd: fixture('named') }).then(result => {
expect(result.out).toEqual('\n' +
' First\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
'\n' +
' Second\n' +
' Package size: 9 B\n' +
' Size limit: 1 KB\n' +
'\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('shows analyzer', () => {
return run(['--why'], { cwd: fixture('unlimit') }).then(result => {
expect(result.out).toContain('Webpack Bundle Analyzer')
expect(result.code).toEqual(0)
})
})
it('shows analyzer for multiple limits', () => {
return run(['--why'], { cwd: fixture('good') }).then(result => {
expect(result.out.match(/Webpack Bundle Analyzer/g)).toHaveLength(1)
expect(result.code).toEqual(0)
})
})
it('returns size', () => {
return run(['test/fixtures/unlimit/empty.js']).then(result => {
expect(result.out).toEqual('\n' +
' Package size: 0 B\n' +
' With all dependencies, minified and gzipped\n' +
'\n')
expect(result.code).toEqual(0)
})
})
it('uses limit in non-config mode', () => {
const file = 'test/fixtures/unlimit/empty.js'
return run(['--limit', '10 KB', file]).then(result => {
expect(result.out).toContain('Size limit: 10 KB\n')
expect(result.code).toEqual(0)
})
})
it('uses different units', () => {
return run(['test/fixtures/bad/index.js']).then(result => {
expect(result.out).toContain('Package size: 2.35 KB\n')
expect(result.code).toEqual(0)
})
})
it('supports absolute path', () => {
const file = path.join(__dirname, 'fixtures/unlimit/empty.js')
return run([file]).then(result => {
expect(result.out).toContain('Package size: 0 B\n')
expect(result.code).toEqual(0)
})
})
it('ignores peerDependencies', () => {
return run([], { cwd: fixture('peer') }).then(result => {
expect(result.out).toContain('Package size: 80 B\n')
expect(result.code).toEqual(0)
})
})
it('disables webpack by argument', () => {
return run(['--no-webpack', 'test/fixtures/bad/index.js']).then(result => {
expect(result.out).toContain('Package size: 37 B\n')
expect(result.code).toEqual(0)
})
})
it('disables webpack by option', () => {
return run([], { cwd: fixture('bundled') }).then(result => {
expect(result.out).toContain('Package size: 37 B\n')
expect(result.code).toEqual(0)
})
})
it('disables gzip by argument', () => {
return run(['--no-gzip', 'test/fixtures/bad/index.js']).then(result => {
expect(result.out).toContain('Package size: 6.85 KB\n')
expect(result.code).toEqual(0)
})
})
it('disables gzip by option', () => {
return run([], { cwd: fixture('gzip') }).then(result => {
expect(result.out).toContain('Package size: 14 B\n')
expect(result.code).toEqual(0)
})
})
it('throws on --why with disabled webpack', () => {
return run(['--why'], { cwd: fixture('bundled') }).then(result => {
expect(result.out).toContain('--why does not work')
expect(result.code).toEqual(1)
})
})
it('uses custom webpack', () => {
return run([], { cwd: fixture('webpack-config') }).then(result => {
expect(result.out).toContain('Package size: 2.26 KB')
expect(result.code).toEqual(0)
})
})
it('uses custom webpack when specified via --config', () => {
return run([
'--config', fixture('webpack-config/webpack.config.js'),
fixture('webpack-config/index.js')
]).then(result => {
expect(result.out).toContain('Package size: 2.43 KB')
expect(result.code).toEqual(0)
})
})