-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
run.test.js
244 lines (200 loc) · 6.77 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
let { join } = require('path')
let run = require('../run')
jest.mock('../../time/get-running-time', () => () => 1)
jest.mock('../../time/cache', () => ({
getCache () {
return false
},
saveCache () { }
}))
const TMP_DIR = /size-limit-[\w-]+\/?/g
const ROOT = join(__dirname, '..', '..', '..')
function fixture (...files) {
return join(__dirname, 'fixtures', ...files)
}
function createProcess (cwd, args = []) {
let history = {
exitCode: 0,
stdout: '',
stderr: ''
}
let process = {
argv: ['node', 'size-limit', ...args],
cwd () {
if (cwd.includes('/')) {
return cwd
} else {
return fixture(cwd)
}
},
exit (code) {
history.exitCode = code
},
stdout: {
write (str) {
history.stdout += str.split(ROOT).join('').replace(TMP_DIR, '')
}
},
stderr: {
write (str) {
history.stderr += str.split(ROOT).join('').replace(TMP_DIR, '')
}
}
}
return [process, history]
}
function normalize (output) {
return output.replace(/"webpackOutput": "[^"]+"/, '"webpackOutput": "/tmp/"')
}
async function check (cwd, args) {
let [process, history] = createProcess(cwd, args)
await run(process)
expect(history.stderr).toEqual('')
expect(history.exitCode).toEqual(0)
return history.stdout
}
async function error (cwd, args) {
let [process, history] = createProcess(cwd, args)
await run(process)
expect(history.stdout).toEqual('')
expect(history.exitCode).toEqual(1)
return history.stderr
}
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('JSONError')
})
it('shows error in JSON format', async () => {
let [process, history] = createProcess('/', ['--json'])
await run(process)
expect(history.exitCode).toEqual(1)
expect(history.stderr).toEqual('')
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 yarn 2 users', async () => {
let [process, history] = createProcess('yarn-without-dev-dep')
await run(process)
expect(history.exitCode).toEqual(0)
expect(history.stderr).toMatchSnapshot()
})
it('shows migration guide for yarn users: config 1, dep 0', async () => {
let [process, history] = createProcess('yarn-with-config-without-dev')
await run(process)
expect(history.exitCode).toEqual(1)
expect(history.stderr).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).toEqual(1)
expect(history.stderr).toMatchSnapshot()
})
it('shows migration guide for npm users, because of yarn 2', async () => {
let [process, history] = createProcess('npm-without-dev-dep')
await run(process)
expect(history.exitCode).toEqual(0)
expect(history.stderr).toMatchSnapshot()
})
it('shows size-limit dependency warning', async () => {
let [process, history] = createProcess('warn')
await run(process)
expect(history.exitCode).toEqual(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 --why argument without webpack', async () => {
expect(await error('file', ['--why'])).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 gzip option without gzip plugin', async () => {
expect(await error('non-gzip')).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('works in integration test with JSON', async () => {
expect(await check('integration', ['--json'])).toMatchSnapshot()
})
it('works in integration test with size', async () => {
expect(await check('integration')).toMatchSnapshot()
})
it('works in integration test with time', async () => {
expect(await check('integration', ['--limit', '2s'])).toMatchSnapshot()
})
it('shows error on time bigger than limit', async () => {
let [process, history] = createProcess('integration', ['--limit', '1 s'])
await run(process)
expect(history.exitCode).toEqual(1)
expect(history.stderr).toEqual('')
expect(history.stdout).toMatchSnapshot()
})
it('returns zero bytes for empty file', async () => {
expect(await check('zero')).toMatchSnapshot()
})
it('returns zero bytes for empty file without gzip', async () => {
expect(await check('zero-non-gzip')).toMatchSnapshot()
})
it('shows debug', async () => {
expect(normalize(await check('integration', ['--debug']))).toMatchSnapshot()
})
it('shows debug on error', async () => {
expect(await error('internal-error', ['--debug'])).toMatchSnapshot()
})