UNPKG

3.39 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.createReporterError = createReporterError;
7exports.createArrayReporterError = createArrayReporterError;
8exports.validateReporters = validateReporters;
9
10function _chalk() {
11 const data = _interopRequireDefault(require('chalk'));
12
13 _chalk = function () {
14 return data;
15 };
16
17 return data;
18}
19
20function _jestGetType() {
21 const data = require('jest-get-type');
22
23 _jestGetType = function () {
24 return data;
25 };
26
27 return data;
28}
29
30function _jestValidate() {
31 const data = require('jest-validate');
32
33 _jestValidate = function () {
34 return data;
35 };
36
37 return data;
38}
39
40var _utils = require('./utils');
41
42function _interopRequireDefault(obj) {
43 return obj && obj.__esModule ? obj : {default: obj};
44}
45
46/**
47 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
48 *
49 * This source code is licensed under the MIT license found in the
50 * LICENSE file in the root directory of this source tree.
51 */
52const validReporterTypes = ['array', 'string'];
53const ERROR = `${_utils.BULLET}Reporter Validation Error`;
54/**
55 * Reporter Validation Error is thrown if the given arguments
56 * within the reporter are not valid.
57 *
58 * This is a highly specific reporter error and in the future will be
59 * merged with jest-validate. Till then, we can make use of it. It works
60 * and that's what counts most at this time.
61 */
62
63function createReporterError(reporterIndex, reporterValue) {
64 const errorMessage =
65 ` Reporter at index ${reporterIndex} must be of type:\n` +
66 ` ${_chalk().default.bold.green(validReporterTypes.join(' or '))}\n` +
67 ` but instead received:\n` +
68 ` ${_chalk().default.bold.red(
69 (0, _jestGetType().getType)(reporterValue)
70 )}`;
71 return new (_jestValidate().ValidationError)(
72 ERROR,
73 errorMessage,
74 _utils.DOCUMENTATION_NOTE
75 );
76}
77
78function createArrayReporterError(
79 arrayReporter,
80 reporterIndex,
81 valueIndex,
82 value,
83 expectedType,
84 valueName
85) {
86 const errorMessage =
87 ` Unexpected value for ${valueName} ` +
88 `at index ${valueIndex} of reporter at index ${reporterIndex}\n` +
89 ' Expected:\n' +
90 ` ${_chalk().default.bold.red(expectedType)}\n` +
91 ' Got:\n' +
92 ` ${_chalk().default.bold.green((0, _jestGetType().getType)(value))}\n` +
93 ` Reporter configuration:\n` +
94 ` ${_chalk().default.bold.green(
95 JSON.stringify(arrayReporter, null, 2).split('\n').join('\n ')
96 )}`;
97 return new (_jestValidate().ValidationError)(
98 ERROR,
99 errorMessage,
100 _utils.DOCUMENTATION_NOTE
101 );
102}
103
104function validateReporters(reporterConfig) {
105 return reporterConfig.every((reporter, index) => {
106 if (Array.isArray(reporter)) {
107 validateArrayReporter(reporter, index);
108 } else if (typeof reporter !== 'string') {
109 throw createReporterError(index, reporter);
110 }
111
112 return true;
113 });
114}
115
116function validateArrayReporter(arrayReporter, reporterIndex) {
117 const [path, options] = arrayReporter;
118
119 if (typeof path !== 'string') {
120 throw createArrayReporterError(
121 arrayReporter,
122 reporterIndex,
123 0,
124 path,
125 'string',
126 'Path'
127 );
128 } else if (typeof options !== 'object') {
129 throw createArrayReporterError(
130 arrayReporter,
131 reporterIndex,
132 1,
133 options,
134 'object',
135 'Reporter Configuration'
136 );
137 }
138}