Skip to content

Commit 73773e2

Browse files
test: validate options (#110)
1 parent 0d77b18 commit 73773e2

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
4+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
5+
- options has an unknown property 'unknown'. These properties are valid:
6+
object {}"
7+
`;
8+
9+
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
10+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
11+
- options has an unknown property 'unknown'. These properties are valid:
12+
object {}"
13+
`;
14+
15+
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
16+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
17+
- options has an unknown property 'unknown'. These properties are valid:
18+
object {}"
19+
`;
20+
21+
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
22+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
23+
- options has an unknown property 'unknown'. These properties are valid:
24+
object {}"
25+
`;
26+
27+
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
28+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
29+
- options has an unknown property 'unknown'. These properties are valid:
30+
object {}"
31+
`;
32+
33+
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
34+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
35+
- options has an unknown property 'unknown'. These properties are valid:
36+
object {}"
37+
`;
38+
39+
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
40+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
41+
- options has an unknown property 'unknown'. These properties are valid:
42+
object {}"
43+
`;
44+
45+
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
46+
"Invalid options object. Source Map Loader has been initialized using an options object that does not match the API schema.
47+
- options has an unknown property 'unknown'. These properties are valid:
48+
object {}"
49+
`;

test/validate-options.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { getCompiler, compile } from './helpers';
2+
3+
describe('validate options', () => {
4+
const tests = {
5+
unknown: {
6+
success: [],
7+
failure: [1, true, false, 'test', /test/, [], {}, { foo: 'bar' }],
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+
const compiler = getCompiler('normal-file.js', { [key]: value });
27+
28+
let stats;
29+
30+
try {
31+
stats = await compile(compiler);
32+
} finally {
33+
if (type === 'success') {
34+
expect(stats.hasErrors()).toBe(false);
35+
} else if (type === 'failure') {
36+
const {
37+
compilation: { errors },
38+
} = stats;
39+
40+
expect(errors).toHaveLength(1);
41+
expect(() => {
42+
throw new Error(errors[0].error.message);
43+
}).toThrowErrorMatchingSnapshot();
44+
}
45+
}
46+
});
47+
}
48+
49+
for (const [key, values] of Object.entries(tests)) {
50+
for (const type of Object.keys(values)) {
51+
for (const value of values[type]) {
52+
createTestCase(key, value, type);
53+
}
54+
}
55+
}
56+
});

0 commit comments

Comments
 (0)