-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathvalidate-plugin-options.test.js
94 lines (86 loc) · 2.25 KB
/
validate-plugin-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
import { version as webpackVersion } from 'webpack';
import MiniCssExtractPlugin from '../src';
describe('validate options', () => {
const tests = {
filename: {
success: [
'[name].css',
({ name }) => `${name.replace('/js/', '/css/')}.css`,
],
failure: [true],
},
chunkFilename: {
success: [
'[id].css',
webpackVersion[0] === '4'
? '[id].[name].css'
: ({ chunk }) => `${chunk.id}.${chunk.name}.css`,
],
failure: [true],
},
emitFile: {
success: [true, false],
failure: [1],
},
ignoreOrder: {
success: [true, false],
failure: [1],
},
insert: {
success: ['#existing-style', function insert() {}],
failure: [1, true, {}],
},
attributes: {
success: [{}, { id: 'id' }],
failure: [true],
},
linkType: {
success: [true, false, 'text/css'],
failure: [1, {}, [], 'invalid/type'],
},
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 () => {
let error;
try {
// eslint-disable-next-line no-new
new MiniCssExtractPlugin({ [key]: value });
} catch (errorFromPlugin) {
if (errorFromPlugin.name !== 'ValidationError') {
throw errorFromPlugin;
}
error = errorFromPlugin;
} finally {
if (type === 'success') {
expect(error).toBeUndefined();
} else if (type === 'failure') {
expect(() => {
throw error;
}).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);
}
}
}
});