UNPKG

1.78 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.errorMessage = void 0;
7function _chalk() {
8 const data = _interopRequireDefault(require('chalk'));
9 _chalk = function () {
10 return data;
11 };
12 return data;
13}
14function _jestGetType() {
15 const data = require('jest-get-type');
16 _jestGetType = function () {
17 return data;
18 };
19 return data;
20}
21var _condition = require('./condition');
22var _utils = require('./utils');
23function _interopRequireDefault(obj) {
24 return obj && obj.__esModule ? obj : {default: obj};
25}
26/**
27 * Copyright (c) Meta Platforms, Inc. and affiliates.
28 *
29 * This source code is licensed under the MIT license found in the
30 * LICENSE file in the root directory of this source tree.
31 */
32
33const errorMessage = (option, received, defaultValue, options, path) => {
34 const conditions = (0, _condition.getValues)(defaultValue);
35 const validTypes = Array.from(
36 new Set(conditions.map(_jestGetType().getType))
37 );
38 const message = ` Option ${_chalk().default.bold(
39 `"${path && path.length > 0 ? `${path.join('.')}.` : ''}${option}"`
40 )} must be of type:
41 ${validTypes.map(e => _chalk().default.bold.green(e)).join(' or ')}
42 but instead received:
43 ${_chalk().default.bold.red((0, _jestGetType().getType)(received))}
44
45 Example:
46${formatExamples(option, conditions)}`;
47 const comment = options.comment;
48 const name = (options.title && options.title.error) || _utils.ERROR;
49 throw new _utils.ValidationError(name, message, comment);
50};
51exports.errorMessage = errorMessage;
52function formatExamples(option, examples) {
53 return examples.map(
54 e => ` {
55 ${_chalk().default.bold(`"${option}"`)}: ${_chalk().default.bold(
56 (0, _utils.formatPrettyObject)(e)
57 )}
58 }`
59 ).join(`
60
61 or
62
63`);
64}