/
serve-basic.test.js
342 lines (270 loc) · 13.8 KB
/
serve-basic.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
'use strict';
const stripAnsi = require('strip-ansi');
const path = require('path');
const getPort = require('get-port');
const { runServe, isWebpack5, isDevServer4 } = require('../../utils/test-utils');
const testPath = path.resolve(__dirname);
describe('basic serve usage', () => {
let port;
beforeEach(async () => {
port = await getPort();
});
const isWindows = process.platform === 'win32';
// TODO fix me on windows
if (isWindows) {
it('TODO: Fix on windows', () => {
expect(true).toBe(true);
});
return;
}
it('should work', async () => {
const { stderr, stdout } = await runServe(__dirname, []);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--config" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'serve.config.js', '--port', port]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--config" and "--env" options', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'--config',
'function-with-env.config.js',
'--env',
'foo=bar',
'--port',
port,
]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('WEBPACK_SERVE: true');
expect(stdout).toContain("foo: 'bar'");
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--config" and "--env" options and expose dev server options', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'--config',
'function-with-argv.config.js',
'--env',
'foo=bar',
'--hot',
'--port',
port,
]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('hot: true');
expect(stdout).toContain('WEBPACK_SERVE: true');
expect(stdout).toContain("foo: 'bar'");
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
});
it('should work in multi compiler mode', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi.config.js', '--port', port]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
expect(stdout).toContain('first-output/main.js');
expect(stdout).toContain('two');
expect(stdout).toContain('second-output/main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
// TODO need fix in future, edge case
it.skip('should work in multi compiler mode with multiple dev servers', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-dev-server.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
expect(stdout).toContain('first-output/main.js');
expect(stdout).toContain('two');
expect(stdout).toContain('second-output/main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--mode" option', async () => {
const { stderr, stdout } = await runServe(__dirname, []);
expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--stats" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--stats']);
expect(stderr).toBeFalsy();
expect(stripAnsi(stdout)).toContain(isWebpack5 ? 'compiled successfully' : 'Version: webpack');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--stats verbose" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--stats', 'verbose']);
expect(stderr).toBeFalsy();
expect(stdout).toContain(isWebpack5 ? 'from webpack.Compiler' : 'webpack.buildChunkGraph.visitModules');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--mode" option #2', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--mode', 'production']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('production');
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--mode" option #3', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--mode', 'development']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--progress" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--progress']);
expect(stderr).toContain('webpack.Progress');
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--progress" option using the "profile" value', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--progress', 'profile']);
expect(stderr).toContain('webpack.Progress');
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--client-log-level" option', async () => {
const { stdout, stderr } = await runServe(testPath, ['--client-log-level', 'info']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--port" option', async () => {
const { stdout, stderr } = await runServe(testPath, ['--port', port]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--hot" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--hot']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
});
it('should work with the "--no-hot" option', async () => {
const { stdout, stderr } = await runServe(testPath, ['--port', port, '--no-hot']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--hot" option using the "only" value', async () => {
const { stdout, stderr } = await runServe(testPath, ['--port', port, isDevServer4 ? '--hot only' : '--hot-only']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
});
it('should work with "--hot" and "--port" options', async () => {
const { stdout, stderr } = await runServe(testPath, ['--port', port, '--hot']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
});
it('should work with the "--hot" and "--progress" options', async () => {
const { stdout, stderr } = await runServe(testPath, ['--port', port, '--hot', '--progress']);
expect(stderr).toContain('webpack.Progress');
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
});
it('should work with the default "publicPath" option', async () => {
const { stderr, stdout } = await runServe(__dirname, []);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stripAnsi(stdout)).toContain('from /');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--output-public-path" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--output-public-path', '/my-public-path/']);
if (isWebpack5) {
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout).toContain('/my-public-path/');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
} else {
expect(stderr).toContain("Error: Unknown option '--output-public-path'");
expect(stdout).toBeFalsy();
}
});
it('should respect the "publicPath" option from configuration', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'output-public-path.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout).toContain('/my-public-path/');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should respect the "publicPath" option from configuration using multi compiler mode', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-output-public-path.config.js', '--port', port]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
expect(stdout).toContain('first-output/main.js');
expect(stdout).toContain('two');
expect(stdout).toContain('second-output/main.js');
expect(stdout).toContain('/my-public-path/');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should respect the "publicPath" option from configuration (from the "devServer" options)', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'dev-server-output-public-path.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout).toContain('/dev-server-my-public-path/');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with the "--open" option', async () => {
const { stdout, stderr } = await runServe(testPath, ['--open', '--port', port]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should respect the "publicPath" option from configuration using multi compiler mode (from the "devServer" options)', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-dev-server-output-public-path.config.js', '--port', port]);
expect(stderr).toBeFalsy();
expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
expect(stdout).toContain('first-output/main.js');
expect(stdout).toContain('two');
expect(stdout).toContain('second-output/main.js');
expect(stdout).toContain('/dev-server-my-public-path/');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work with entries syntax', async () => {
const { stderr, stdout } = await runServe(__dirname, ['./src/entry.js', '--port', port]);
expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should work and log warning on the `watch option in a configuration', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', './watch.config.js', '--port', port]);
expect(stderr).toContain(
"No need to use the 'serve' command together with '{ watch: true }' configuration, it does not make sense.",
);
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});
it('should log used supplied config with serve', async () => {
const { stderr, stdout } = await runServe(__dirname, ['--config', 'log.config.js', '--port', port]);
const configPath = path.resolve(__dirname, './log.config.js');
expect(stderr).toContain('Compiler starting...');
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
expect(stderr).toContain('Compiler finished');
expect(stdout).toBeTruthy();
});
it("should log error on using '--watch' flag with serve", async () => {
const { stdout, stderr } = await runServe(testPath, ['--watch']);
expect(stderr).toContain("Error: Unknown option '--watch'");
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
expect(stdout).toBeFalsy();
});
it("should log error on using '-w' alias with serve", async () => {
const { stdout, stderr } = await runServe(testPath, ['-w']);
expect(stderr).toContain("Error: Unknown option '-w'");
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
expect(stdout).toBeFalsy();
});
it('should log an error on unknown flag', async () => {
const { exitCode, stdout, stderr } = await runServe(testPath, ['--port', port, '--unknown-flag']);
expect(exitCode).toBe(2);
expect(stderr).toContain("Error: Unknown option '--unknown-flag'");
expect(stdout).toBeFalsy();
});
});