UNPKG

@webpack-contrib/defaults

Version:

Project configuration and boilerplate defaults for webpack projects

79 lines (73 loc) 2.15 kB
import { getCompiler, compile } from './helpers'; describe('validate options', () => { const tests = { name: { 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 () => { // For loaders // 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 plugins // let error; // // try { // // eslint-disable-next-line no-new // new Plugin({ [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); } } } });