1 | import { getCompiler, compile } from './helpers';
|
2 |
|
3 | describe('validate options', () => {
|
4 | const tests = {
|
5 | name: {
|
6 | success: [1, true, false, 'test', /test/, [], {}, { foo: 'bar' }],
|
7 | failure: [],
|
8 | },
|
9 | };
|
10 |
|
11 | function stringifyValue(value) {
|
12 | if (
|
13 | Array.isArray(value) ||
|
14 | (value && typeof value === 'object' && value.constructor === Object)
|
15 | ) {
|
16 | return JSON.stringify(value);
|
17 | }
|
18 |
|
19 | return value;
|
20 | }
|
21 |
|
22 | async function createTestCase(key, value, type) {
|
23 | it(`should ${
|
24 | type === 'success' ? 'successfully validate' : 'throw an error on'
|
25 | } the "${key}" option with "${stringifyValue(value)}" value`, async () => {
|
26 | // For loaders
|
27 | // const compiler = getCompiler('simple.js', { [key]: value });
|
28 | //
|
29 | // let stats;
|
30 | //
|
31 | // try {
|
32 | // stats = await compile(compiler);
|
33 | // } finally {
|
34 | // if (type === 'success') {
|
35 | // expect(stats.hasErrors()).toBe(false);
|
36 | // } else if (type === 'failure') {
|
37 | // const {
|
38 | // compilation: { errors },
|
39 | // } = stats;
|
40 | //
|
41 | // expect(errors).toHaveLength(1);
|
42 | // expect(() => {
|
43 | // throw new Error(errors[0].error.message);
|
44 | // }).toThrowErrorMatchingSnapshot();
|
45 | // }
|
46 | // }
|
47 | // For plugins
|
48 | // let error;
|
49 | //
|
50 | // try {
|
51 | // // eslint-disable-next-line no-new
|
52 | // new Plugin({ [key]: value });
|
53 | // } catch (errorFromPlugin) {
|
54 | // if (errorFromPlugin.name !== 'ValidationError') {
|
55 | // throw errorFromPlugin;
|
56 | // }
|
57 | //
|
58 | // error = errorFromPlugin;
|
59 | // } finally {
|
60 | // if (type === 'success') {
|
61 | // expect(error).toBeUndefined();
|
62 | // } else if (type === 'failure') {
|
63 | // expect(() => {
|
64 | // throw error;
|
65 | // }).toThrowErrorMatchingSnapshot();
|
66 | // }
|
67 | // }
|
68 | });
|
69 | }
|
70 |
|
71 | for (const [key, values] of Object.entries(tests)) {
|
72 | for (const type of Object.keys(values)) {
|
73 | for (const value of values[type]) {
|
74 | createTestCase(key, value, type);
|
75 | }
|
76 | }
|
77 | }
|
78 | });
|