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
|
import { getCompiler, compile } from './helpers';
describe('validate options', () => {
const tests = {
limit: {
success: [8192, true, '8192'],
failure: [{}, []],
},
mimetype: {
success: ['image/png', 'unknown/unknown', true, false],
failure: [() => {}],
},
fallback: {
success: [
// 'custom-loader',
{ loader: 'custom-loader' },
{
loader: 'custom-loader',
options: { unknown: 'unknown' },
},
],
failure: [true],
},
esModule: {
success: [true, false],
failure: ['true'],
},
unknown: {
success: [1, true, false, 'test', /test/, [], {}, { foo: 'bar' }],
failure: [],
},
};
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 compiler = getCompiler('simple.js', { [key]: value });
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);
}
}
}
});
|