-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
Validation.test.js
408 lines (399 loc) · 13.7 KB
/
Validation.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* globals describe, it */
"use strict";
require("should");
const webpack = require("../lib/webpack");
describe("Validation", () => {
const testCases = [
{
name: "undefined configuration",
config: undefined,
message: [" - configuration should be an object."]
},
{
name: "null configuration",
config: null,
message: [" - configuration should be an object."]
},
{
name: "empty entry string",
config: {
entry: ""
},
message: [
" - configuration.entry should be one of these:",
" object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function",
" -> The entry point(s) of the compilation.",
" Details:",
" * configuration.entry should be an object.",
" -> Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.",
" * configuration.entry should not be empty.",
" -> An entry point without name. The string is resolved to a module which is loaded upon startup.",
" * configuration.entry should be an array:",
" [non-empty string]",
" * configuration.entry should be an instance of function",
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things."
]
},
{
name: "empty entry bundle array",
config: {
entry: {
bundle: []
}
},
message: [
" - configuration.entry should be one of these:",
" object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function",
" -> The entry point(s) of the compilation.",
" Details:",
" * configuration.entry['bundle'] should be a string.",
" -> The string is resolved to a module which is loaded upon startup.",
" * configuration.entry['bundle'] should not be empty.",
" * configuration.entry should be a string.",
" -> An entry point without name. The string is resolved to a module which is loaded upon startup.",
" * configuration.entry should be an array:",
" [non-empty string]",
" * configuration.entry should be an instance of function",
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things."
]
},
{
name: "invalid instanceof",
config: {
entry: "a",
module: {
wrappedContextRegExp: 1337
}
},
message: [
" - configuration.module.wrappedContextRegExp should be an instance of RegExp",
" -> Set the inner regular expression for partial dynamic dependencies"
]
},
{
name: "invalid minimum",
config: {
entry: "a",
parallelism: 0
},
message: [
" - configuration.parallelism should be >= 1.",
" -> The number of parallel processed modules in the compilation."
]
},
{
name: "repeated value",
config: {
entry: ["abc", "def", "abc"]
},
message: [
" - configuration.entry should be one of these:",
" object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function",
" -> The entry point(s) of the compilation.",
" Details:",
" * configuration.entry should be an object.",
" -> Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.",
" * configuration.entry should be a string.",
" -> An entry point without name. The string is resolved to a module which is loaded upon startup.",
" * configuration.entry should not contain the item 'abc' twice.",
" * configuration.entry should be an instance of function",
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things."
]
},
{
name: "multiple errors",
config: {
entry: [/a/],
output: {
filename: /a/
}
},
message: [
" - configuration.entry should be one of these:",
" object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function",
" -> The entry point(s) of the compilation.",
" Details:",
" * configuration.entry should be an object.",
" -> Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.",
" * configuration.entry should be a string.",
" -> An entry point without name. The string is resolved to a module which is loaded upon startup.",
" * configuration.entry[0] should be a string.",
" -> A non-empty string",
" * configuration.entry should be an instance of function",
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things.",
" - configuration.output.filename should be one of these:",
" string | function",
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
" Details:",
" * configuration.output.filename should be a string.",
" * configuration.output.filename should be an instance of function"
]
},
{
name: "multiple configurations",
config: [
{
entry: [/a/]
},
{
entry: "a",
output: {
filename: /a/
}
}
],
message: [
" - configuration[0].entry should be one of these:",
" object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] | function",
" -> The entry point(s) of the compilation.",
" Details:",
" * configuration[0].entry should be an object.",
" -> Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.",
" * configuration[0].entry should be a string.",
" -> An entry point without name. The string is resolved to a module which is loaded upon startup.",
" * configuration[0].entry[0] should be a string.",
" -> A non-empty string",
" * configuration[0].entry should be an instance of function",
" -> A Function returning an entry object, an entry string, an entry array or a promise to these things.",
" - configuration[1].output.filename should be one of these:",
" string | function",
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
" Details:",
" * configuration[1].output.filename should be a string.",
" * configuration[1].output.filename should be an instance of function"
]
},
{
name: "deep error",
config: {
entry: "a",
module: {
rules: [
{
oneOf: [
{
test: "/a",
passer: {
amd: false
}
}
]
}
]
}
},
message: [
" - configuration.module.rules[0].oneOf[0] has an unknown property 'passer'. These properties are valid:",
" object { enforce?, exclude?, include?, issuer?, loader?, loaders?, oneOf?, options?, parser?, resolve?, sideEffects?, query?, type?, resource?, resourceQuery?, compiler?, rules?, test?, use? }",
" -> A rule"
]
},
{
name: "additional key on root",
config: {
entry: "a",
postcss: () => {}
},
message: [
" - configuration has an unknown property 'postcss'. These properties are valid:",
" object { mode?, amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, externals?, " +
"loader?, module?, name?, node?, output?, optimization?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, " +
"recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, stats?, target?, watch?, watchOptions? }",
" For typos: please correct them.",
" For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.",
" Loaders should be updated to allow passing options via loader options in module.rules.",
" Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:",
" plugins: [",
" new webpack.LoaderOptionsPlugin({",
" // test: /\\.xxx$/, // may apply this only for some modules",
" options: {",
" postcss: ...",
" }",
" })",
" ]"
]
},
{
name: "enum",
config: {
entry: "a",
devtool: true
},
message: [
" - configuration.devtool should be one of these:",
" string | false",
" -> A developer tool to enhance debugging.",
" Details:",
" * configuration.devtool should be a string.",
" * configuration.devtool should be false"
]
},
{
name: "! in path",
config: {
entry: "foo.js",
output: {
path: "/somepath/!test",
filename: "bar"
}
},
message: [
' - configuration.output.path: The provided value "/somepath/!test" contans exclamation mark (!) which is not allowed because it\'s reserved for loader syntax.',
" -> The output directory as **absolute path** (required)."
]
},
{
name: "relative path",
config: {
entry: "foo.js",
output: {
filename: "/bar"
}
},
message: [
' - configuration.output.filename: A relative path is expected. However, the provided value "/bar" is an absolute path!',
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
" Please use output.path to specify absolute path and output.filename for the file name."
]
},
{
name: "absolute path",
config: {
entry: "foo.js",
output: {
filename: "bar"
},
context: "baz"
},
message: [
' - configuration.context: The provided value "baz" is not an absolute path!',
" -> The base directory (absolute path!) for resolving the `entry` option. If `output.pathinfo` is set, the included pathinfo is shortened to this directory."
]
},
{
name: "missing stats option",
config: {
entry: "foo.js",
stats: {
foobar: true
}
},
test(err) {
err.message.should.startWith("Invalid configuration object.");
err.message
.split("\n")
.slice(1)[0]
.should.be.eql(" - configuration.stats should be one of these:");
}
},
{
name: "Invalid plugin provided: bool",
config: {
entry: "foo.js",
plugins: [false]
},
message: [
" - configuration.plugins[0] should be one of these:",
" object { apply, ... } | function",
" -> Plugin of type object or instanceof Function",
" Details:",
" * configuration.plugins[0] should be an object.",
" -> Plugin instance",
" * configuration.plugins[0] should be an instance of function",
" -> Function acting as plugin"
]
},
{
name: "Invalid plugin provided: array",
config: {
entry: "foo.js",
plugins: [[]]
},
message: [
" - configuration.plugins[0] should be one of these:",
" object { apply, ... } | function",
" -> Plugin of type object or instanceof Function",
" Details:",
" * configuration.plugins[0] should be an object.",
" -> Plugin instance",
" * configuration.plugins[0] should be an instance of function",
" -> Function acting as plugin"
]
},
{
name: "Invalid plugin provided: string",
config: {
entry: "foo.js",
plugins: ["abc123"]
},
message: [
" - configuration.plugins[0] should be one of these:",
" object { apply, ... } | function",
" -> Plugin of type object or instanceof Function",
" Details:",
" * configuration.plugins[0] should be an object.",
" -> Plugin instance",
" * configuration.plugins[0] should be an instance of function",
" -> Function acting as plugin"
]
},
{
name: "Invalid plugin provided: int",
config: {
entry: "foo.js",
plugins: [12]
},
message: [
" - configuration.plugins[0] should be one of these:",
" object { apply, ... } | function",
" -> Plugin of type object or instanceof Function",
" Details:",
" * configuration.plugins[0] should be an object.",
" -> Plugin instance",
" * configuration.plugins[0] should be an instance of function",
" -> Function acting as plugin"
]
},
{
name: "Invalid plugin provided: object without apply function",
config: {
entry: "foo.js",
plugins: [{}]
},
message: [
" - configuration.plugins[0] should be one of these:",
" object { apply, ... } | function",
" -> Plugin of type object or instanceof Function",
" Details:",
" * configuration.plugins[0] misses the property 'apply'.",
" function",
" -> The run point of the plugin, required method.",
" * configuration.plugins[0] misses the property 'apply'.",
" function",
" -> The run point of the plugin, required method.",
" * configuration.plugins[0] should be an instance of function",
" -> Function acting as plugin"
]
}
];
testCases.forEach(testCase => {
it("should fail validation for " + testCase.name, () => {
try {
webpack(testCase.config);
} catch (err) {
if (err.name !== "WebpackOptionsValidationError") throw err;
if (testCase.test) {
testCase.test(err);
return;
}
err.message.should.startWith("Invalid configuration object.");
err.message
.split("\n")
.slice(1)
.should.be.eql(testCase.message);
return;
}
throw new Error("Validation didn't fail");
});
});
});