jest_matchers_object.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. // Global matchers object holds the list of available matchers and
  6. // the state, that can hold matcher specific values that change over time.
  7. const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
  8. // Notes a built-in/internal Jest matcher.
  9. // Jest may override the stack trace of Errors thrown by internal matchers.
  10. /**
  11. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  12. *
  13. * This source code is licensed under the MIT license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. *
  16. *
  17. */
  18. const INTERNAL_MATCHER_FLAG = exports.INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher');
  19. if (!global[JEST_MATCHERS_OBJECT]) {
  20. Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
  21. value: {
  22. matchers: Object.create(null),
  23. state: {
  24. assertionCalls: 0,
  25. expectedAssertionsNumber: null,
  26. isExpectingAssertions: false,
  27. suppressedErrors: [] // errors that are not thrown immediately.
  28. }
  29. }
  30. });
  31. }
  32. const getState = exports.getState = () => global[JEST_MATCHERS_OBJECT].state;
  33. const setState = exports.setState = state => {
  34. Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
  35. };
  36. const getMatchers = exports.getMatchers = () => global[JEST_MATCHERS_OBJECT].matchers;
  37. const setMatchers = exports.setMatchers = (matchers, isInternal) => {
  38. Object.keys(matchers).forEach(key => {
  39. const matcher = matchers[key];
  40. Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
  41. value: isInternal
  42. });
  43. });
  44. Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
  45. };