Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add loader options validation (#234)
- Loading branch information
1 parent
dcf4daa
commit 6980095
Showing
5 changed files
with
182 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`validate options should throw an error on the "sourceMap" option with "string" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options.sourceMap should be a boolean. | ||
-> Enables/Disables generation of source maps (https://github.com/webpack-contrib/stylus-loader#sourcemap)." | ||
`; | ||
|
||
exports[`validate options should throw an error on the "stylusOptions" option with "[]" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options.stylusOptions should be an object: | ||
object { … } | ||
-> Options to pass through to \`Stylus\` (https://github.com/webpack-contrib/stylus-loader#stylusoptions)." | ||
`; | ||
|
||
exports[`validate options should throw an error on the "stylusOptions" option with "1" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options.stylusOptions should be an object: | ||
object { … } | ||
-> Options to pass through to \`Stylus\` (https://github.com/webpack-contrib/stylus-loader#stylusoptions)." | ||
`; | ||
|
||
exports[`validate options should throw an error on the "stylusOptions" option with "false" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options.stylusOptions should be an object: | ||
object { … } | ||
-> Options to pass through to \`Stylus\` (https://github.com/webpack-contrib/stylus-loader#stylusoptions)." | ||
`; | ||
|
||
exports[`validate options should throw an error on the "stylusOptions" option with "test" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options.stylusOptions should be an object: | ||
object { … } | ||
-> Options to pass through to \`Stylus\` (https://github.com/webpack-contrib/stylus-loader#stylusoptions)." | ||
`; | ||
|
||
exports[`validate options should throw an error on the "stylusOptions" option with "true" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options.stylusOptions should be an object: | ||
object { … } | ||
-> Options to pass through to \`Stylus\` (https://github.com/webpack-contrib/stylus-loader#stylusoptions)." | ||
`; | ||
|
||
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; | ||
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` | ||
"Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema. | ||
- options has an unknown property 'unknown'. These properties are valid: | ||
object { stylusOptions?, sourceMap? }" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { getCompiler, compile } from './helpers/index'; | ||
|
||
describe('validate options', () => { | ||
const tests = { | ||
stylusOptions: { | ||
success: [ | ||
{}, | ||
{ resolveCss: true }, | ||
{ includeCSS: false }, | ||
{ | ||
define: { | ||
$development: process.env.NODE_ENV === 'development', | ||
}, | ||
}, | ||
], | ||
failure: [1, true, false, 'test', []], | ||
}, | ||
sourceMap: { | ||
success: [true, false], | ||
failure: ['string'], | ||
}, | ||
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 () => { | ||
const compiler = getCompiler('./basic.styl', { | ||
[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); | ||
} | ||
} | ||
} | ||
}); |