jestMatchersObject.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.setState =
  6. exports.setMatchers =
  7. exports.getState =
  8. exports.getMatchers =
  9. exports.INTERNAL_MATCHER_FLAG =
  10. void 0;
  11. var _asymmetricMatchers = require('./asymmetricMatchers');
  12. var global = (function () {
  13. if (typeof globalThis !== 'undefined') {
  14. return globalThis;
  15. } else if (typeof global !== 'undefined') {
  16. return global;
  17. } else if (typeof self !== 'undefined') {
  18. return self;
  19. } else if (typeof window !== 'undefined') {
  20. return window;
  21. } else {
  22. return Function('return this')();
  23. }
  24. })();
  25. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  26. // Global matchers object holds the list of available matchers and
  27. // the state, that can hold matcher specific values that change over time.
  28. const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object'); // Notes a built-in/internal Jest matcher.
  29. // Jest may override the stack trace of Errors thrown by internal matchers.
  30. const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher');
  31. exports.INTERNAL_MATCHER_FLAG = INTERNAL_MATCHER_FLAG;
  32. if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
  33. const defaultState = {
  34. assertionCalls: 0,
  35. expectedAssertionsNumber: null,
  36. isExpectingAssertions: false,
  37. suppressedErrors: [] // errors that are not thrown immediately.
  38. };
  39. Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
  40. value: {
  41. matchers: Object.create(null),
  42. state: defaultState
  43. }
  44. });
  45. }
  46. const getState = () => global[JEST_MATCHERS_OBJECT].state;
  47. exports.getState = getState;
  48. const setState = state => {
  49. Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
  50. };
  51. exports.setState = setState;
  52. const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;
  53. exports.getMatchers = getMatchers;
  54. const setMatchers = (matchers, isInternal, expect) => {
  55. Object.keys(matchers).forEach(key => {
  56. const matcher = matchers[key];
  57. Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
  58. value: isInternal
  59. });
  60. if (!isInternal) {
  61. // expect is defined
  62. class CustomMatcher extends _asymmetricMatchers.AsymmetricMatcher {
  63. constructor(inverse = false, ...sample) {
  64. super(sample, inverse);
  65. }
  66. asymmetricMatch(other) {
  67. const {pass} = matcher.call(
  68. this.getMatcherContext(),
  69. other,
  70. ...this.sample
  71. );
  72. return this.inverse ? !pass : pass;
  73. }
  74. toString() {
  75. return `${this.inverse ? 'not.' : ''}${key}`;
  76. }
  77. getExpectedType() {
  78. return 'any';
  79. }
  80. toAsymmetricMatcher() {
  81. return `${this.toString()}<${this.sample.map(String).join(', ')}>`;
  82. }
  83. }
  84. Object.defineProperty(expect, key, {
  85. configurable: true,
  86. enumerable: true,
  87. value: (...sample) => new CustomMatcher(false, ...sample),
  88. writable: true
  89. });
  90. Object.defineProperty(expect.not, key, {
  91. configurable: true,
  92. enumerable: true,
  93. value: (...sample) => new CustomMatcher(true, ...sample),
  94. writable: true
  95. });
  96. }
  97. });
  98. Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
  99. };
  100. exports.setMatchers = setMatchers;