reporter_validation_errors.js 7.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /**
  6. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  7. *
  8. * This source code is licensed under the MIT license found in the
  9. * LICENSE file in the root directory of this source tree.
  10. *
  11. */
  12. exports.createReporterError = createReporterError;
  13. exports.createArrayReporterError = createArrayReporterError;
  14. exports.validateReporters = validateReporters;
  15. var _jestValidate;
  16. function _load_jestValidate() {
  17. return _jestValidate = require('jest-validate');
  18. }
  19. var _chalk;
  20. function _load_chalk() {
  21. return _chalk = _interopRequireDefault(require('chalk'));
  22. }
  23. var _jestGetType;
  24. function _load_jestGetType() {
  25. return _jestGetType = _interopRequireDefault(require('jest-get-type'));
  26. }
  27. var _utils;
  28. function _load_utils() {
  29. return _utils = require('./utils');
  30. }
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  32. const validReporterTypes = ['array', 'string'];
  33. const ERROR = `${(_utils || _load_utils()).BULLET}Reporter Validation Error`;
  34. /**
  35. * Reporter Validation Error is thrown if the given arguments
  36. * within the reporter are not valid.
  37. *
  38. * This is a highly specific reporter error and in the future will be
  39. * merged with jest-validate. Till then, we can make use of it. It works
  40. * and that's what counts most at this time.
  41. */
  42. function createReporterError(reporterIndex, reporterValue) {
  43. const errorMessage = ` Reporter at index ${reporterIndex} must be of type:\n` + ` ${(_chalk || _load_chalk()).default.bold.green(validReporterTypes.join(' or '))}\n` + ` but instead received:\n` + ` ${(_chalk || _load_chalk()).default.bold.red((0, (_jestGetType || _load_jestGetType()).default)(reporterValue))}`;
  44. return new (_jestValidate || _load_jestValidate()).ValidationError(ERROR, errorMessage, (_utils || _load_utils()).DOCUMENTATION_NOTE);
  45. }
  46. function createArrayReporterError(arrayReporter, reporterIndex, valueIndex, value, expectedType, valueName) {
  47. const errorMessage = ` Unexpected value for ${valueName} ` + `at index ${valueIndex} of reporter at index ${reporterIndex}\n` + ' Expected:\n' + ` ${(_chalk || _load_chalk()).default.bold.red(expectedType)}\n` + ' Got:\n' + ` ${(_chalk || _load_chalk()).default.bold.green((0, (_jestGetType || _load_jestGetType()).default)(value))}\n` + ` Reporter configuration:\n` + ` ${(_chalk || _load_chalk()).default.bold.green(JSON.stringify(arrayReporter, null, 2).split('\n').join('\n '))}`;
  48. return new (_jestValidate || _load_jestValidate()).ValidationError(ERROR, errorMessage, (_utils || _load_utils()).DOCUMENTATION_NOTE);
  49. }
  50. function validateReporters(reporterConfig) {
  51. return reporterConfig.every((reporter, index) => {
  52. if (Array.isArray(reporter)) {
  53. validateArrayReporter(reporter, index);
  54. } else if (typeof reporter !== 'string') {
  55. throw createReporterError(index, reporter);
  56. }
  57. return true;
  58. });
  59. }
  60. function validateArrayReporter(arrayReporter, reporterIndex) {
  61. var _arrayReporter = _slicedToArray(arrayReporter, 2);
  62. const path = _arrayReporter[0],
  63. options = _arrayReporter[1];
  64. if (typeof path !== 'string') {
  65. throw createArrayReporterError(arrayReporter, reporterIndex, 0, path, 'string', 'Path');
  66. } else if (typeof options !== 'object') {
  67. throw createArrayReporterError(arrayReporter, reporterIndex, 1, options, 'object', 'Reporter Configuration');
  68. }
  69. }