/
githubmirror
/
jest
Обзор
Документация
Войти
/
githubmirror
/
jest
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/jest-validate/src/errors.ts
50 строк
1 KB
Christoph Nakazawa
Stop using `import X = require('…')`. (#15659)
06 июн 2025, 02:49
Не верифицирован
06 июн 2025, 02:49
a2218e4
Код
Авторство
О чём код?
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import chalk from 'chalk'; import {getType} from '@jest/get-type'; import {getValues} from './condition'; import type {ValidationOptions} from './types'; import {ERROR, ValidationError, formatPrettyObject} from './utils'; export const errorMessage = ( option: string, received: unknown, defaultValue: unknown, options: ValidationOptions, path?: Array<string>, ): void => { const conditions = getValues(defaultValue); const validTypes: Array<string> = [...new Set(conditions.map(getType))]; const message = ` Option ${chalk.bold( `"${path && path.length > 0 ? `${path.join('.')}.` : ''}${option}"`, )} must be of type: ${validTypes.map(e => chalk.bold.green(e)).join(' or ')} but instead received: ${chalk.bold.red(getType(received))} Example: ${formatExamples(option, conditions)}`; const comment = options.comment; const name = (options.title && options.title.error) || ERROR; throw new ValidationError(name, message, comment); }; function formatExamples(option: string, examples: Array<unknown>) { return examples.map( e => ` { ${chalk.bold(`"${option}"`)}: ${chalk.bold(formatPrettyObject(e))} }`, ).join(` or `); }