state.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.setState =
  6. exports.resetState =
  7. exports.getState =
  8. exports.dispatchSync =
  9. exports.dispatch =
  10. exports.addEventHandler =
  11. exports.ROOT_DESCRIBE_BLOCK_NAME =
  12. void 0;
  13. var _eventHandler = _interopRequireDefault(require('./eventHandler'));
  14. var _formatNodeAssertErrors = _interopRequireDefault(
  15. require('./formatNodeAssertErrors')
  16. );
  17. var _types = require('./types');
  18. var _utils = require('./utils');
  19. function _interopRequireDefault(obj) {
  20. return obj && obj.__esModule ? obj : {default: obj};
  21. }
  22. /**
  23. * Copyright (c) Meta Platforms, Inc. and affiliates.
  24. *
  25. * This source code is licensed under the MIT license found in the
  26. * LICENSE file in the root directory of this source tree.
  27. */
  28. const eventHandlers = [_eventHandler.default, _formatNodeAssertErrors.default];
  29. const ROOT_DESCRIBE_BLOCK_NAME = 'ROOT_DESCRIBE_BLOCK';
  30. exports.ROOT_DESCRIBE_BLOCK_NAME = ROOT_DESCRIBE_BLOCK_NAME;
  31. const createState = () => {
  32. const ROOT_DESCRIBE_BLOCK = (0, _utils.makeDescribe)(
  33. ROOT_DESCRIBE_BLOCK_NAME
  34. );
  35. return {
  36. currentDescribeBlock: ROOT_DESCRIBE_BLOCK,
  37. currentlyRunningTest: null,
  38. expand: undefined,
  39. hasFocusedTests: false,
  40. hasStarted: false,
  41. includeTestLocationInResult: false,
  42. maxConcurrency: 5,
  43. parentProcess: null,
  44. rootDescribeBlock: ROOT_DESCRIBE_BLOCK,
  45. seed: 0,
  46. testNamePattern: null,
  47. testTimeout: 5000,
  48. unhandledErrors: []
  49. };
  50. };
  51. /* eslint-disable no-restricted-globals */
  52. const resetState = () => {
  53. global[_types.STATE_SYM] = createState();
  54. };
  55. exports.resetState = resetState;
  56. resetState();
  57. const getState = () => global[_types.STATE_SYM];
  58. exports.getState = getState;
  59. const setState = state => (global[_types.STATE_SYM] = state);
  60. /* eslint-enable */
  61. exports.setState = setState;
  62. const dispatch = async event => {
  63. for (const handler of eventHandlers) {
  64. await handler(event, getState());
  65. }
  66. };
  67. exports.dispatch = dispatch;
  68. const dispatchSync = event => {
  69. for (const handler of eventHandlers) {
  70. handler(event, getState());
  71. }
  72. };
  73. exports.dispatchSync = dispatchSync;
  74. const addEventHandler = handler => {
  75. eventHandlers.push(handler);
  76. };
  77. exports.addEventHandler = addEventHandler;