jest_matchers_object.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.setMatchers = exports.getMatchers = exports.setState = exports.getState = exports.INTERNAL_MATCHER_FLAG = undefined;
  6. var _asymmetric_matchers = require('./asymmetric_matchers');
  7. function _toConsumableArray(arr) {
  8. if (Array.isArray(arr)) {
  9. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++)
  10. arr2[i] = arr[i];
  11. return arr2;
  12. } else {
  13. return Array.from(arr);
  14. }
  15. }
  16. /**
  17. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  18. *
  19. * This source code is licensed under the MIT license found in the
  20. * LICENSE file in the root directory of this source tree.
  21. *
  22. *
  23. */
  24. // Global matchers object holds the list of available matchers and
  25. // the state, that can hold matcher specific values that change over time.
  26. const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
  27. // Notes a built-in/internal Jest matcher.
  28. // Jest may override the stack trace of Errors thrown by internal matchers.
  29. const INTERNAL_MATCHER_FLAG = (exports.INTERNAL_MATCHER_FLAG = Symbol.for(
  30. '$$jest-internal-matcher'
  31. ));
  32. if (!global[JEST_MATCHERS_OBJECT]) {
  33. Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
  34. value: {
  35. matchers: Object.create(null),
  36. state: {
  37. assertionCalls: 0,
  38. expectedAssertionsNumber: null,
  39. isExpectingAssertions: false,
  40. suppressedErrors: [] // errors that are not thrown immediately.
  41. }
  42. }
  43. });
  44. }
  45. const getState = (exports.getState = () => global[JEST_MATCHERS_OBJECT].state);
  46. const setState = (exports.setState = state => {
  47. Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
  48. });
  49. const getMatchers = (exports.getMatchers = () =>
  50. global[JEST_MATCHERS_OBJECT].matchers);
  51. const setMatchers = (exports.setMatchers = (matchers, isInternal, expect) => {
  52. Object.keys(matchers).forEach(key => {
  53. const matcher = matchers[key];
  54. Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
  55. value: isInternal
  56. });
  57. if (!isInternal) {
  58. // expect is defined
  59. class CustomMatcher extends _asymmetric_matchers.AsymmetricMatcher {
  60. constructor() {
  61. let inverse =
  62. arguments.length > 0 && arguments[0] !== undefined
  63. ? arguments[0]
  64. : false;
  65. super();
  66. this.inverse = inverse;
  67. for (
  68. var _len = arguments.length,
  69. sample = Array(_len > 1 ? _len - 1 : 0),
  70. _key = 1;
  71. _key < _len;
  72. _key++
  73. ) {
  74. sample[_key - 1] = arguments[_key];
  75. }
  76. this.sample = sample;
  77. }
  78. asymmetricMatch(other) {
  79. var _ref = matcher.apply(
  80. undefined,
  81. [other].concat(_toConsumableArray(this.sample))
  82. );
  83. const pass = _ref.pass;
  84. return this.inverse ? !pass : pass;
  85. }
  86. toString() {
  87. return `${this.inverse ? 'not.' : ''}${key}`;
  88. }
  89. getExpectedType() {
  90. return 'any';
  91. }
  92. toAsymmetricMatcher() {
  93. return `${this.toString()}<${this.sample.join(', ')}>`;
  94. }
  95. }
  96. expect[key] = function() {
  97. for (
  98. var _len2 = arguments.length, sample = Array(_len2), _key2 = 0;
  99. _key2 < _len2;
  100. _key2++
  101. ) {
  102. sample[_key2] = arguments[_key2];
  103. }
  104. return new (Function.prototype.bind.apply(
  105. CustomMatcher,
  106. [null].concat([false], sample)
  107. ))();
  108. };
  109. if (!expect.not) {
  110. expect.not = {};
  111. }
  112. expect.not[key] = function() {
  113. for (
  114. var _len3 = arguments.length, sample = Array(_len3), _key3 = 0;
  115. _key3 < _len3;
  116. _key3++
  117. ) {
  118. sample[_key3] = arguments[_key3];
  119. }
  120. return new (Function.prototype.bind.apply(
  121. CustomMatcher,
  122. [null].concat([true], sample)
  123. ))();
  124. };
  125. }
  126. });
  127. Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
  128. });