expectation_result_factory.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = expectationResultFactory;
  6. var _prettyFormat = require('pretty-format');
  7. var _prettyFormat2 = _interopRequireDefault(_prettyFormat);
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. function messageFormatter(_ref) {
  12. let error = _ref.error,
  13. message = _ref.message,
  14. passed = _ref.passed;
  15. if (passed) {
  16. return 'Passed.';
  17. }
  18. if (message) {
  19. return message;
  20. }
  21. if (typeof error === 'string') {
  22. return error;
  23. }
  24. if (
  25. // duck-type Error, see #2549
  26. error &&
  27. typeof error === 'object' &&
  28. typeof error.message === 'string' &&
  29. typeof error.name === 'string'
  30. ) {
  31. return `${error.name}: ${error.message}`;
  32. }
  33. return `thrown: ${(0, _prettyFormat2.default)(error, {maxDepth: 3})}`;
  34. }
  35. /**
  36. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  37. *
  38. * This source code is licensed under the MIT license found in the
  39. * LICENSE file in the root directory of this source tree.
  40. *
  41. *
  42. */
  43. function stackFormatter(options, initError, errorMessage) {
  44. if (options.passed) {
  45. return '';
  46. }
  47. if (options.error) {
  48. if (options.error.stack) {
  49. return options.error.stack;
  50. }
  51. if (options.error === errorMessage) {
  52. return errorMessage;
  53. }
  54. }
  55. if (initError) {
  56. return errorMessage.trimRight() + '\n\n' + initError.stack;
  57. }
  58. return new Error(errorMessage).stack;
  59. }
  60. function expectationResultFactory(options, initError) {
  61. const message = messageFormatter(options);
  62. const stack = stackFormatter(options, initError, message);
  63. if (options.passed) {
  64. return {
  65. error: options.error,
  66. matcherName: options.matcherName,
  67. message: message,
  68. passed: options.passed,
  69. stack: stack
  70. };
  71. }
  72. return {
  73. actual: options.actual,
  74. error: options.error,
  75. expected: options.expected,
  76. matcherName: options.matcherName,
  77. message: message,
  78. passed: options.passed,
  79. stack: stack
  80. };
  81. }