treeProcessor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = treeProcessor;
  6. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  7. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  8. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  9. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  10. try {
  11. var info = gen[key](arg);
  12. var value = info.value;
  13. } catch (error) {
  14. reject(error);
  15. return;
  16. }
  17. if (info.done) {
  18. resolve(value);
  19. } else {
  20. Promise.resolve(value).then(_next, _throw);
  21. }
  22. }
  23. function _asyncToGenerator(fn) {
  24. return function() {
  25. var self = this,
  26. args = arguments;
  27. return new Promise(function(resolve, reject) {
  28. var gen = fn.apply(self, args);
  29. function _next(value) {
  30. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
  31. }
  32. function _throw(err) {
  33. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
  34. }
  35. _next(undefined);
  36. });
  37. };
  38. }
  39. /**
  40. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  41. *
  42. * This source code is licensed under the MIT license found in the
  43. * LICENSE file in the root directory of this source tree.
  44. */
  45. function treeProcessor(options) {
  46. const nodeComplete = options.nodeComplete,
  47. nodeStart = options.nodeStart,
  48. queueRunnerFactory = options.queueRunnerFactory,
  49. runnableIds = options.runnableIds,
  50. tree = options.tree;
  51. function isEnabled(node, parentEnabled) {
  52. return parentEnabled || runnableIds.indexOf(node.id) !== -1;
  53. }
  54. function getNodeHandler(node, parentEnabled) {
  55. const enabled = isEnabled(node, parentEnabled);
  56. return node.children
  57. ? getNodeWithChildrenHandler(node, enabled)
  58. : getNodeWithoutChildrenHandler(node, enabled);
  59. }
  60. function getNodeWithoutChildrenHandler(node, enabled) {
  61. return function fn(done = () => {}) {
  62. node.execute(done, enabled);
  63. };
  64. }
  65. function getNodeWithChildrenHandler(node, enabled) {
  66. return (
  67. /*#__PURE__*/
  68. (function() {
  69. var _fn = _asyncToGenerator(function*(done = () => {}) {
  70. nodeStart(node);
  71. yield queueRunnerFactory({
  72. onException: error => node.onException(error),
  73. queueableFns: wrapChildren(node, enabled),
  74. userContext: node.sharedUserContext()
  75. });
  76. nodeComplete(node);
  77. done();
  78. });
  79. function fn() {
  80. return _fn.apply(this, arguments);
  81. }
  82. return fn;
  83. })()
  84. );
  85. }
  86. function hasEnabledTest(node) {
  87. if (node.children) {
  88. return node.children.some(hasEnabledTest);
  89. }
  90. return !node.disabled;
  91. }
  92. function wrapChildren(node, enabled) {
  93. if (!node.children) {
  94. throw new Error('`node.children` is not defined.');
  95. }
  96. const children = node.children.map(child => ({
  97. fn: getNodeHandler(child, enabled)
  98. }));
  99. if (!hasEnabledTest(node)) {
  100. return children;
  101. }
  102. return node.beforeAllFns.concat(children).concat(node.afterAllFns);
  103. }
  104. const treeHandler = getNodeHandler(tree, false);
  105. return treeHandler();
  106. }