ReporterValidationErrors.js 3.3 KB

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