testGroups.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var testGroups_exports = {};
  20. __export(testGroups_exports, {
  21. createTestGroups: () => createTestGroups,
  22. filterForShard: () => filterForShard
  23. });
  24. module.exports = __toCommonJS(testGroups_exports);
  25. function createTestGroups(projectSuite, expectedParallelism) {
  26. const groups = /* @__PURE__ */ new Map();
  27. const createGroup = (test) => {
  28. return {
  29. workerHash: test._workerHash,
  30. requireFile: test._requireFile,
  31. repeatEachIndex: test.repeatEachIndex,
  32. projectId: test._projectId,
  33. tests: []
  34. };
  35. };
  36. for (const test of projectSuite.allTests()) {
  37. let withWorkerHash = groups.get(test._workerHash);
  38. if (!withWorkerHash) {
  39. withWorkerHash = /* @__PURE__ */ new Map();
  40. groups.set(test._workerHash, withWorkerHash);
  41. }
  42. let withRequireFile = withWorkerHash.get(test._requireFile);
  43. if (!withRequireFile) {
  44. withRequireFile = {
  45. general: createGroup(test),
  46. parallel: /* @__PURE__ */ new Map(),
  47. parallelWithHooks: createGroup(test)
  48. };
  49. withWorkerHash.set(test._requireFile, withRequireFile);
  50. }
  51. let insideParallel = false;
  52. let outerMostSequentialSuite;
  53. let hasAllHooks = false;
  54. for (let parent = test.parent; parent; parent = parent.parent) {
  55. if (parent._parallelMode === "serial" || parent._parallelMode === "default")
  56. outerMostSequentialSuite = parent;
  57. insideParallel = insideParallel || parent._parallelMode === "parallel";
  58. hasAllHooks = hasAllHooks || parent._hooks.some((hook) => hook.type === "beforeAll" || hook.type === "afterAll");
  59. }
  60. if (insideParallel) {
  61. if (hasAllHooks && !outerMostSequentialSuite) {
  62. withRequireFile.parallelWithHooks.tests.push(test);
  63. } else {
  64. const key = outerMostSequentialSuite || test;
  65. let group = withRequireFile.parallel.get(key);
  66. if (!group) {
  67. group = createGroup(test);
  68. withRequireFile.parallel.set(key, group);
  69. }
  70. group.tests.push(test);
  71. }
  72. } else {
  73. withRequireFile.general.tests.push(test);
  74. }
  75. }
  76. const result = [];
  77. for (const withWorkerHash of groups.values()) {
  78. for (const withRequireFile of withWorkerHash.values()) {
  79. if (withRequireFile.general.tests.length)
  80. result.push(withRequireFile.general);
  81. result.push(...withRequireFile.parallel.values());
  82. const parallelWithHooksGroupSize = Math.ceil(withRequireFile.parallelWithHooks.tests.length / expectedParallelism);
  83. let lastGroup;
  84. for (const test of withRequireFile.parallelWithHooks.tests) {
  85. if (!lastGroup || lastGroup.tests.length >= parallelWithHooksGroupSize) {
  86. lastGroup = createGroup(test);
  87. result.push(lastGroup);
  88. }
  89. lastGroup.tests.push(test);
  90. }
  91. }
  92. }
  93. return result;
  94. }
  95. function filterForShard(shard, testGroups) {
  96. let shardableTotal = 0;
  97. for (const group of testGroups)
  98. shardableTotal += group.tests.length;
  99. const shardSize = Math.floor(shardableTotal / shard.total);
  100. const extraOne = shardableTotal - shardSize * shard.total;
  101. const currentShard = shard.current - 1;
  102. const from = shardSize * currentShard + Math.min(extraOne, currentShard);
  103. const to = from + shardSize + (currentShard < extraOne ? 1 : 0);
  104. let current = 0;
  105. const result = /* @__PURE__ */ new Set();
  106. for (const group of testGroups) {
  107. if (current >= from && current < to)
  108. result.add(group);
  109. current += group.tests.length;
  110. }
  111. return result;
  112. }
  113. // Annotate the CommonJS export names for ESM import in node:
  114. 0 && (module.exports = {
  115. createTestGroups,
  116. filterForShard
  117. });