快速验证
通用配置验证工具, 用于帮助您处理警告、 错误、 贬值消息以及显示用户正确配置的例子 .
npm install --save jest-validate
使用量
import {validate} from 'jest-validate';
validate(config, validationOptions); // => {hasDeprecationWarnings: boolean, isValid: boolean}
何处 ValidationOptions 它们是:
type ValidationOptions = {
comment?: string;
condition?: (option: unknown, validOption: unknown) => boolean;
deprecate?: (
config: Record<string, unknown>,
option: string,
deprecatedOptions: DeprecatedOptions,
options: ValidationOptions,
) => boolean;
deprecatedConfig?: DeprecatedOptions;
error?: (
option: string,
received: unknown,
defaultValue: unknown,
options: ValidationOptions,
path?: Array<string>,
) => void;
exampleConfig: Record<string, unknown>;
recursive?: boolean;
recursiveBlacklist?: Array<string>;
recursiveDenylist?: Array<string>;
title?: Title;
unknown?: (
config: Record<string, unknown>,
exampleConfig: Record<string, unknown>,
option: string,
options: ValidationOptions,
path?: Array<string>,
) => void;
};
type Title = {
deprecation?: string;
error?: string;
warning?: string;
};
exampleConfig 唯一的选项.
API 密码
默认 jest-validate 将打印通用警告和错误消息。 您可以通过提供 options: ValidationOptions 对象作为第二个参数 :
几乎任何东西都可以被覆盖 以满足你的需求.
选项
recursiveDenylist– 字符串键盘的可选数组,应当排除于深(反向)验证.comment— 在错误/ 警告消息下可选择的字符串 .condition– 具有验证条件的可选函数.deprecate, (中文(简体) ),error, (中文(简体) ),unknown- 用于显示警告和错误消息的可选功能.deprecatedConfig– 带有已贬值配置密钥的可选对象.exampleConfig- 唯一一个 所需经费 选项,您要测试其配置.recursive- 确定是否递归比较的可选布尔exampleConfig改为config(默认 :true) (中文(简体) ).title- 错误和消息标题的可选对象.
你会找到实例 condition, (中文(简体) ), deprecate, (中文(简体) ), error, (中文(简体) ), unknown,以及 deprecatedConfig 此寄存器的内源, 分别命名 .
示例
exampleConfig 应为包含每个密钥的有效值示例的有密钥/值对的对象。 配置值在 :
- 它与示例值的JavaScript类型相匹配,例如.
string, (中文(简体) ),number, (中文(简体) ),array, (中文(简体) ),boolean, (中文(简体) ),function,或object - 这是
null或者说undefined - 它符合传递给任何参数的 Javascript 类型
MultipleValidOptions(...)
最后一个条件是一个特殊的语法,允许在允许一个以上类型的情况下进行验证;参见下面的例子. 在示例中拥有多个同类型值是可以接受的,所以也可以使用这个语法提供多个实例. 当验证失败发生时,错误消息会将阵列中的所有其他值显示为示例.
实例
最小实例 :
validate(config, {exampleConfig});
略作修改的例子:
validate(config, {
comment: ' Documentation: http://custom-docs.com',
deprecatedConfig,
exampleConfig,
title: {
deprecation: 'Custom Deprecation',
// leaving 'error' and 'warning' as default
},
});
这将输出 :
警告:
● Validation Warning:
Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
This is either a typing error or a user mistake. Fixing it will remove this message.
Documentation: http://custom-docs.com
错误 :
● Validation Error:
Option transform must be of type:
object
but instead received:
string
Example:
{
"transform": {
"\\.js$": "<rootDir>/preprocessor.js"
}
}
Documentation: http://custom-docs.com
实例验证多个类型
import {multipleValidOptions} from 'jest-validate';
validate(config, {
// `bar` will accept either a string or a number
bar: multipleValidOptions('string is ok', 2),
});
错误 :
● Validation Error:
Option foo must be of type:
string or number
but instead received:
array
Example:
{
"bar": "string is ok"
}
or
{
"bar": 2
}
Documentation: http://custom-docs.com
折旧
依据 deprecatedConfig 对象,并带有适当的折旧信息。 注自定义标题 :
Custom Deprecation:
Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.
Jest now treats your current configuration as:
{
"transform": {".*": "xxx"}
}
Please update your configuration.
Documentation: http://custom-docs.com
实例验证 CLI 参数
import {validate} from 'jest-validate';
validateCLIOptions(argv, {...allowedOptions, deprecatedOptions});
若为 argv 包含一个未在 allowedOptions, (中文(简体) ), validateCLIOptions 将随信件在 deprecatedOptions 配置 :
● collectCoverageOnlyFrom:
Option "collectCoverageOnlyFrom" was replaced by "collectCoverageFrom"
CLI Options Documentation: https://jestjs.io/docs/en/cli.html
如果折旧选项仍列在 allowedOptions 配置,然后 validateCLIOptions 将打印警告而不显示错误.