ReporterValidationErrors.js 4.5 KB

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