tree_processor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = treeProcessor;
  6. function _asyncToGenerator(fn) {
  7. return function() {
  8. var gen = fn.apply(this, arguments);
  9. return new Promise(function(resolve, reject) {
  10. function step(key, arg) {
  11. try {
  12. var info = gen[key](arg);
  13. var value = info.value;
  14. } catch (error) {
  15. reject(error);
  16. return;
  17. }
  18. if (info.done) {
  19. resolve(value);
  20. } else {
  21. return Promise.resolve(value).then(
  22. function(value) {
  23. step('next', value);
  24. },
  25. function(err) {
  26. step('throw', err);
  27. }
  28. );
  29. }
  30. }
  31. return step('next');
  32. });
  33. };
  34. }
  35. // Try getting the real promise object from the context, if available. Someone
  36. // could have overridden it in a test. Async functions return it implicitly.
  37. // eslint-disable-next-line no-unused-vars
  38. /**
  39. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  40. *
  41. * This source code is licensed under the MIT license found in the
  42. * LICENSE file in the root directory of this source tree.
  43. *
  44. *
  45. */
  46. const Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  47. function treeProcessor(options) {
  48. const nodeComplete = options.nodeComplete,
  49. nodeStart = options.nodeStart,
  50. queueRunnerFactory = options.queueRunnerFactory,
  51. runnableIds = options.runnableIds,
  52. tree = options.tree;
  53. function isEnabled(node, parentEnabled) {
  54. return parentEnabled || runnableIds.indexOf(node.id) !== -1;
  55. }
  56. function getNodeHandler(node, parentEnabled) {
  57. const enabled = isEnabled(node, parentEnabled);
  58. return node.children
  59. ? getNodeWithChildrenHandler(node, enabled)
  60. : getNodeWithoutChildrenHandler(node, enabled);
  61. }
  62. function getNodeWithoutChildrenHandler(node, enabled) {
  63. return function fn() {
  64. let done =
  65. arguments.length > 0 && arguments[0] !== undefined
  66. ? arguments[0]
  67. : () => {};
  68. node.execute(done, enabled);
  69. };
  70. }
  71. function getNodeWithChildrenHandler(node, enabled) {
  72. return (() => {
  73. var _ref = _asyncToGenerator(function*() {
  74. let done =
  75. arguments.length > 0 && arguments[0] !== undefined
  76. ? arguments[0]
  77. : function() {};
  78. nodeStart(node);
  79. yield queueRunnerFactory({
  80. onException: function(error) {
  81. return node.onException(error);
  82. },
  83. queueableFns: wrapChildren(node, enabled),
  84. userContext: node.sharedUserContext()
  85. });
  86. nodeComplete(node);
  87. done();
  88. });
  89. function fn() {
  90. return _ref.apply(this, arguments);
  91. }
  92. return fn;
  93. })();
  94. }
  95. function hasEnabledTest(node) {
  96. if (node.children) {
  97. if (node.children.some(hasEnabledTest)) {
  98. return true;
  99. }
  100. } else {
  101. return !node.disabled;
  102. }
  103. return false;
  104. }
  105. function wrapChildren(node, enabled) {
  106. if (!node.children) {
  107. throw new Error('`node.children` is not defined.');
  108. }
  109. const children = node.children.map(child => ({
  110. fn: getNodeHandler(child, enabled)
  111. }));
  112. if (!hasEnabledTest(node)) {
  113. return children;
  114. }
  115. return node.beforeAllFns.concat(children).concat(node.afterAllFns);
  116. }
  117. const treeHandler = getNodeHandler(tree, false);
  118. return treeHandler();
  119. }