ReporterValidationErrors.js 4.1 KB

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