tree_processor.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = treeProcessor;
  6. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
  7. // Try getting the real promise object from the context, if available. Someone
  8. // could have overridden it in a test. Async functions return it implicitly.
  9. // eslint-disable-next-line no-unused-vars
  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 Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  19. function treeProcessor(options) {
  20. const nodeComplete = options.nodeComplete,
  21. nodeStart = options.nodeStart,
  22. queueRunnerFactory = options.queueRunnerFactory,
  23. runnableIds = options.runnableIds,
  24. tree = options.tree;
  25. function isEnabled(node, parentEnabled) {
  26. return parentEnabled || runnableIds.indexOf(node.id) !== -1;
  27. }
  28. return queueRunnerFactory({
  29. onException: error => tree.onException(error),
  30. queueableFns: wrapChildren(tree, isEnabled(tree, false)),
  31. userContext: tree.sharedUserContext()
  32. });
  33. function executeNode(node, parentEnabled) {
  34. const enabled = isEnabled(node, parentEnabled);
  35. if (!node.children) {
  36. return {
  37. fn(done) {
  38. node.execute(done, enabled);
  39. }
  40. };
  41. }
  42. return {
  43. fn(done) {
  44. return _asyncToGenerator(function* () {
  45. nodeStart(node);
  46. yield queueRunnerFactory({
  47. onException: function (error) {
  48. return node.onException(error);
  49. },
  50. queueableFns: wrapChildren(node, enabled),
  51. userContext: node.sharedUserContext()
  52. });
  53. nodeComplete(node);
  54. done();
  55. })();
  56. }
  57. };
  58. }
  59. function wrapChildren(node, enabled) {
  60. if (!node.children) {
  61. throw new Error('`node.children` is not defined.');
  62. }
  63. const children = node.children.map(child => executeNode(child, enabled));
  64. return node.beforeAllFns.concat(children).concat(node.afterAllFns);
  65. }
  66. }