Skip to content

Commit 7c2f5e7

Browse files
test: code coverage (#2609)
1 parent debe93b commit 7c2f5e7

File tree

7 files changed

+281
-4
lines changed

7 files changed

+281
-4
lines changed

test/api/CLI.test.js

+203-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('CLI API', () => {
9292
command.parseAsync(['--no-boolean'], { from: 'user' });
9393
});
9494

95-
it('should make command with configs option', async (done) => {
95+
it('should make command with configs boolean option', async (done) => {
9696
cli.program.commands = [];
9797

9898
const command = await cli.makeCommand(
@@ -101,7 +101,7 @@ describe('CLI API', () => {
101101
},
102102
[
103103
{
104-
name: 'boolean',
104+
name: 'configs-boolean',
105105
configs: [
106106
{
107107
type: 'boolean',
@@ -111,13 +111,212 @@ describe('CLI API', () => {
111111
},
112112
],
113113
(options) => {
114-
expect(options).toEqual({ boolean: false });
114+
expect(options).toEqual({ configsBoolean: false });
115115

116116
done();
117117
},
118118
);
119119

120-
command.parseAsync(['--no-boolean'], { from: 'user' });
120+
command.parseAsync(['--no-configs-boolean'], { from: 'user' });
121+
});
122+
123+
it('should make command with configs number option', async (done) => {
124+
cli.program.commands = [];
125+
126+
const command = await cli.makeCommand(
127+
{
128+
name: 'command',
129+
},
130+
[
131+
{
132+
name: 'configs-number',
133+
configs: [
134+
{
135+
type: 'number',
136+
},
137+
],
138+
description: 'description',
139+
},
140+
],
141+
(options) => {
142+
expect(options).toEqual({ configsNumber: 42 });
143+
144+
done();
145+
},
146+
);
147+
148+
command.parseAsync(['--configs-number', '42'], { from: 'user' });
149+
});
150+
151+
it('should make command with configs string option', async (done) => {
152+
cli.program.commands = [];
153+
154+
const command = await cli.makeCommand(
155+
{
156+
name: 'command',
157+
},
158+
[
159+
{
160+
name: 'configs-string',
161+
configs: [
162+
{
163+
type: 'string',
164+
},
165+
],
166+
description: 'description',
167+
},
168+
],
169+
(options) => {
170+
expect(options).toEqual({ configsString: 'foo' });
171+
172+
done();
173+
},
174+
);
175+
176+
command.parseAsync(['--configs-string', 'foo'], { from: 'user' });
177+
});
178+
179+
it('should make command with configs path option', async (done) => {
180+
cli.program.commands = [];
181+
182+
const command = await cli.makeCommand(
183+
{
184+
name: 'command',
185+
},
186+
[
187+
{
188+
name: 'configs-path',
189+
configs: [
190+
{
191+
type: 'path',
192+
},
193+
],
194+
description: 'description',
195+
},
196+
],
197+
(options) => {
198+
expect(options).toEqual({ configsPath: '/root/foo' });
199+
200+
done();
201+
},
202+
);
203+
204+
command.parseAsync(['--configs-path', '/root/foo'], { from: 'user' });
205+
});
206+
207+
it('should make command with configs RegExp option', async (done) => {
208+
cli.program.commands = [];
209+
210+
const command = await cli.makeCommand(
211+
{
212+
name: 'command',
213+
},
214+
[
215+
{
216+
name: 'configs-regexp',
217+
configs: [
218+
{
219+
type: 'RegExp',
220+
},
221+
],
222+
description: 'description',
223+
},
224+
],
225+
(options) => {
226+
expect(options).toEqual({ configsRegexp: '\\w+' });
227+
228+
done();
229+
},
230+
);
231+
232+
command.parseAsync(['--configs-regexp', '\\w+'], { from: 'user' });
233+
});
234+
235+
it('should make command with configs enum/string option', async (done) => {
236+
cli.program.commands = [];
237+
238+
const command = await cli.makeCommand(
239+
{
240+
name: 'command',
241+
},
242+
[
243+
{
244+
name: 'enum-string',
245+
configs: [
246+
{
247+
type: 'enum',
248+
values: ['foo'],
249+
},
250+
],
251+
description: 'description',
252+
},
253+
],
254+
(options) => {
255+
expect(options).toEqual({ enumString: 'foo' });
256+
257+
done();
258+
},
259+
);
260+
261+
command.parseAsync(['--enum-string', 'foo'], { from: 'user' });
262+
});
263+
264+
it('should make command with configs enum/number option', async (done) => {
265+
cli.program.commands = [];
266+
267+
const command = await cli.makeCommand(
268+
{
269+
name: 'command',
270+
},
271+
[
272+
{
273+
name: 'enum-number',
274+
configs: [
275+
{
276+
type: 'enum',
277+
values: [42],
278+
},
279+
],
280+
description: 'description',
281+
},
282+
],
283+
(options) => {
284+
expect(options).toEqual({ enumNumber: 42 });
285+
286+
done();
287+
},
288+
);
289+
290+
command.parseAsync(['--enum-number', '42'], { from: 'user' });
291+
});
292+
293+
it('should make command with configs enum/boolean option', async (done) => {
294+
cli.program.commands = [];
295+
296+
const command = await cli.makeCommand(
297+
{
298+
name: 'command',
299+
},
300+
[
301+
{
302+
name: 'enum-boolean',
303+
configs: [
304+
{
305+
type: 'boolean',
306+
values: [false],
307+
},
308+
],
309+
description: 'description',
310+
},
311+
],
312+
(options) => {
313+
expect(options).toEqual({ enumBoolean: false });
314+
315+
done();
316+
},
317+
);
318+
319+
command.parseAsync(['--no-enum-boolean'], { from: 'user' });
121320
});
122321

123322
it('should make command with Boolean option and negative value #2', async (done) => {

test/build/unknown/unknown.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ describe('unknown behaviour', () => {
2121
expect(stdout).toBeFalsy();
2222
});
2323

24+
it('should log an error if an unknown flag is passed and includes =', async () => {
25+
const { exitCode, stderr, stdout } = await run(__dirname, ['--unknown=foo']);
26+
27+
expect(exitCode).toBe(2);
28+
expect(stderr).toContain("Error: Unknown option '--unknown=foo'");
29+
expect(stderr).toContain("Run 'webpack --help' to see available commands and options");
30+
expect(stdout).toBeFalsy();
31+
});
32+
2433
it('should log an error if an unknown flag is passed #3', async () => {
2534
const { exitCode, stderr, stdout } = await run(__dirname, ['-u', '--unknown']);
2635

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = [
2+
{
3+
name: 'one',
4+
mode: 'development',
5+
devtool: false,
6+
output: {
7+
filename: 'first-output/[name].js',
8+
},
9+
devServer: {
10+
port: 8081,
11+
},
12+
},
13+
{
14+
name: 'two',
15+
mode: 'development',
16+
devtool: false,
17+
entry: './src/other.js',
18+
output: {
19+
filename: 'second-output/[name].js',
20+
},
21+
devServer: {
22+
port: 8081,
23+
},
24+
},
25+
];

test/serve/basic/serve-basic.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,19 @@ describe('basic serve usage', () => {
339339
expect(stderr).toContain("Error: Unknown option '--unknown-flag'");
340340
expect(stdout).toBeFalsy();
341341
});
342+
343+
it('should work with the "stats" option in config', async () => {
344+
const { stderr, stdout } = await runWatch(__dirname, ['serve', '--config', 'stats.config.js'], {}, /Compiled successfully/);
345+
346+
expect(stderr).toBeFalsy();
347+
expect(stdout).toContain('Compiled successfully');
348+
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
349+
});
350+
351+
it('should throw error when same ports in multicompiler', async () => {
352+
const { stderr, stdout } = await runWatch(__dirname, ['serve', '--config', 'same-ports-dev-serever.config.js']);
353+
354+
expect(stdout).toBeFalsy();
355+
expect(stderr).toContain('Unique ports must be specified for each devServer option in your webpack configuration');
356+
});
342357
});

test/serve/basic/stats.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
mode: 'development',
3+
devtool: false,
4+
devServer: {
5+
stats: 'minimal',
6+
},
7+
};

test/serve/invalid-schema/invalid-schema.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,20 @@ describe('invalid schema', () => {
2424

2525
expect(stdout).toBeFalsy();
2626
});
27+
28+
it('should log webpack-dev-server error and exit process on invalid flag', async () => {
29+
const { exitCode, stderr, stdout } = await run(__dirname, ['serve', '--port', '-1']);
30+
31+
expect(exitCode).toEqual(2);
32+
expect(stderr).toContain('RangeError');
33+
expect(stdout).toBeFalsy();
34+
});
35+
36+
it('should log webpack-dev-server error and exit process on invalid config', async () => {
37+
const { exitCode, stderr, stdout } = await run(__dirname, ['serve', '--config', './webpack-dev-server.config.mock.js']);
38+
39+
expect(exitCode).toEqual(2);
40+
expect(stderr).toContain('webpack Dev Server Invalid Options');
41+
expect(stdout).toBeFalsy();
42+
});
2743
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
mode: 'development',
3+
devServer: {
4+
bonjour: '',
5+
},
6+
};

0 commit comments

Comments
 (0)