jestMatchersObject.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
  16. value: {
  17. matchers: Object.create(null),
  18. state: {
  19. assertionCalls: 0,
  20. expectedAssertionsNumber: null,
  21. isExpectingAssertions: false,
  22. suppressedErrors: [] // errors that are not thrown immediately.
  23. }
  24. }
  25. });
  26. }
  27. const getState = () => global[JEST_MATCHERS_OBJECT].state;
  28. exports.getState = getState;
  29. const setState = state => {
  30. Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
  31. };
  32. exports.setState = setState;
  33. const getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;
  34. exports.getMatchers = getMatchers;
  35. const setMatchers = (matchers, isInternal, expect) => {
  36. Object.keys(matchers).forEach(key => {
  37. const matcher = matchers[key];
  38. Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
  39. value: isInternal
  40. });
  41. if (!isInternal) {
  42. // expect is defined
  43. class CustomMatcher extends _asymmetricMatchers.AsymmetricMatcher {
  44. constructor(inverse = false, ...sample) {
  45. super(sample);
  46. this.inverse = inverse;
  47. }
  48. asymmetricMatch(other) {
  49. const _ref = matcher(other, ...this.sample),
  50. pass = _ref.pass;
  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.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;