-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathvalidate-options.test.js
155 lines (145 loc) · 4.19 KB
/
validate-options.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
import { getCompiler, compile } from "./helpers/index";
describe("validate options", () => {
const tests = {
url: {
success: [true, false, { filter: () => true }],
failure: ["true", [], () => {}, { filter: true }, { unknown: () => {} }],
},
import: {
success: [true, false, { filter: () => true }],
failure: ["true", [], () => {}, { filter: true }, { unknown: () => {} }],
},
modules: {
success: [
true,
false,
"global",
"local",
"pure",
"icss",
{ mode: "global" },
{ mode: "local" },
{ mode: "pure" },
{ mode: "icss" },
{ mode: () => "local" },
{ localIdentName: "[path][name]__[local]--[hash:base64:5]" },
{ localIdentContext: "context" },
{ localIdentHashSalt: "hash" },
{ localIdentHashFunction: "md4" },
{ localIdentHashDigest: "base64" },
{ localIdentHashDigestLength: 3 },
{
getLocalIdent: (loaderContext, localIdentName, localName) =>
localName,
},
{ localIdentRegExp: "page-(.*)\\.js" },
{ localIdentRegExp: /page-(.*)\.js/ },
{ exportGlobals: true },
{ auto: true },
{ auto: false },
{ auto: /custom-regex/ },
{ auto: () => true },
{ exportLocalsConvention: "asIs" },
{ exportLocalsConvention: "camelCase" },
{ exportLocalsConvention: "camelCaseOnly" },
{ exportLocalsConvention: "dashes" },
{ exportLocalsConvention: "dashesOnly" },
{
exportLocalsConvention: (localName) =>
`${localName.replace(/-/g, "_")}`,
},
{ namedExport: true },
{ namedExport: false },
{ exportOnlyLocals: true },
{ exportOnlyLocals: false },
],
failure: [
"true",
"globals",
"locals",
"pures",
{ mode: true },
{ mode: "globals" },
{ mode: "locals" },
{ mode: "pures" },
{ localIdentName: true },
{ localIdentContext: true },
{ localIdentHashSalt: true },
{ getLocalIdent: [] },
{ localIdentRegExp: true },
{ exportGlobals: "invalid" },
{ auto: "invalid" },
{ exportLocalsConvention: "unknown" },
{ namedExport: "invalid" },
{ exportOnlyLocals: "invalid" },
],
},
sourceMap: {
success: [true, false],
failure: ["true"],
},
importLoaders: {
success: [false, 0, 1, 2, "1"],
failure: [2.5],
},
esModule: {
success: [true, false],
failure: ["true"],
},
exportType: {
success: ["array", "string", "css-style-sheet"],
failure: ["true", false],
},
unknown: {
success: [],
failure: [1, true, false, "test", /test/, [], {}, { foo: "bar" }],
},
};
function stringifyValue(value) {
if (
Array.isArray(value) ||
(value && typeof value === "object" && value.constructor === Object)
) {
return JSON.stringify(value);
}
return value;
}
async function createTestCase(key, value, type) {
it(`should ${
type === "success" ? "successfully validate" : "throw an error on"
} the "${key}" option with "${stringifyValue(value)}" value`, async () => {
const options = { [key]: value };
if (
key === "modules" &&
typeof value === "object" &&
value.namedExport === true
) {
options.esModule = true;
}
const compiler = getCompiler("simple.js", options);
let stats;
try {
stats = await compile(compiler);
} finally {
if (type === "success") {
expect(stats.hasErrors()).toBe(false);
} else if (type === "failure") {
const {
compilation: { errors },
} = stats;
expect(errors).toHaveLength(1);
expect(() => {
throw new Error(errors[0].error.message);
}).toThrowErrorMatchingSnapshot();
}
}
});
}
for (const [key, values] of Object.entries(tests)) {
for (const type of Object.keys(values)) {
for (const value of values[type]) {
createTestCase(key, value, type);
}
}
}
});