jestMatchersObject.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = void 0;
  6. var _asymmetricMatchers = require('./asymmetricMatchers');
  7. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  8. // Global matchers object holds the list of available matchers and
  9. // the state, that can hold matcher specific values that change over time.
  10. const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object'); // Notes a built-in/internal Jest matcher.
  11. // Jest may override the stack trace of Errors thrown by internal matchers.
  12. const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher');
  13. exports.INTERNAL_MATCHER_FLAG = INTERNAL_MATCHER_FLAG;
  14. if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
  15. const defaultState = {
  16. assertionCalls: 0,
  17. expectedAssertionsNumber: null,
  18. isExpectingAssertions: false,
  19. suppressedErrors: [] // errors that are not thrown immediately.
  20. };
  21. Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
  22. value: {
  23. matchers: Object.create(null),
  24. state: defaultState
  25. }
  26. });
  27. }
  28. const getState = () => global[JEST_MATCHERS_OBJECT].state;
  29. exports.getState = getState;
  30. const setState = state => {
  31. Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
  32. };
  33. exports.setState = setState;
  34. const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;
  35. exports.getMatchers = getMatchers;
  36. const setMatchers = (matchers, isInternal, expect) => {
  37. Object.keys(matchers).forEach(key => {
  38. const matcher = matchers[key];
  39. Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
  40. value: isInternal
  41. });
  42. if (!isInternal) {
  43. // expect is defined
  44. class CustomMatcher extends _asymmetricMatchers.AsymmetricMatcher {
  45. constructor(inverse = false, ...sample) {
  46. super(sample);
  47. this.inverse = inverse;
  48. }
  49. asymmetricMatch(other) {
  50. const {pass} = matcher(other, ...this.sample);
  51. return this.inverse ? !pass : pass;
  52. }
  53. toString() {
  54. return `${this.inverse ? 'not.' : ''}${key}`;
  55. }
  56. getExpectedType() {
  57. return 'any';
  58. }
  59. toAsymmetricMatcher() {
  60. return `${this.toString()}<${this.sample.map(String).join(', ')}>`;
  61. }
  62. }
  63. expect[key] = (...sample) => new CustomMatcher(false, ...sample);
  64. if (!expect.not) {
  65. expect.not = {};
  66. }
  67. expect.not[key] = (...sample) => new CustomMatcher(true, ...sample);
  68. }
  69. });
  70. Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
  71. };
  72. exports.setMatchers = setMatchers;