testTree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 testTree_exports = {};
  20. __export(testTree_exports, {
  21. TestTree: () => TestTree,
  22. collectTestIds: () => collectTestIds,
  23. sortAndPropagateStatus: () => sortAndPropagateStatus,
  24. statusEx: () => statusEx
  25. });
  26. module.exports = __toCommonJS(testTree_exports);
  27. class TestTree {
  28. constructor(rootFolder, rootSuite, loadErrors, projectFilters, pathSeparator) {
  29. this._treeItemById = /* @__PURE__ */ new Map();
  30. this._treeItemByTestId = /* @__PURE__ */ new Map();
  31. const filterProjects = projectFilters && [...projectFilters.values()].some(Boolean);
  32. this.pathSeparator = pathSeparator;
  33. this.rootItem = {
  34. kind: "group",
  35. subKind: "folder",
  36. id: rootFolder,
  37. title: "",
  38. location: { file: "", line: 0, column: 0 },
  39. duration: 0,
  40. parent: void 0,
  41. children: [],
  42. status: "none",
  43. hasLoadErrors: false
  44. };
  45. this._treeItemById.set(rootFolder, this.rootItem);
  46. const visitSuite = (project, parentSuite, parentGroup) => {
  47. for (const suite of parentSuite.suites) {
  48. if (!suite.title) {
  49. visitSuite(project, suite, parentGroup);
  50. continue;
  51. }
  52. let group = parentGroup.children.find((item) => item.kind === "group" && item.title === suite.title);
  53. if (!group) {
  54. group = {
  55. kind: "group",
  56. subKind: "describe",
  57. id: "suite:" + parentSuite.titlePath().join("") + "" + suite.title,
  58. // account for anonymous suites
  59. title: suite.title,
  60. location: suite.location,
  61. duration: 0,
  62. parent: parentGroup,
  63. children: [],
  64. status: "none",
  65. hasLoadErrors: false
  66. };
  67. this._addChild(parentGroup, group);
  68. }
  69. visitSuite(project, suite, group);
  70. }
  71. for (const test of parentSuite.tests) {
  72. const title = test.title;
  73. let testCaseItem = parentGroup.children.find((t) => t.kind !== "group" && t.title === title);
  74. if (!testCaseItem) {
  75. testCaseItem = {
  76. kind: "case",
  77. id: "test:" + test.titlePath().join(""),
  78. title,
  79. parent: parentGroup,
  80. children: [],
  81. tests: [],
  82. location: test.location,
  83. duration: 0,
  84. status: "none",
  85. project: void 0,
  86. test: void 0,
  87. tags: test.tags
  88. };
  89. this._addChild(parentGroup, testCaseItem);
  90. }
  91. const result = test.results[0];
  92. let status = "none";
  93. if (result?.[statusEx] === "scheduled")
  94. status = "scheduled";
  95. else if (result?.[statusEx] === "running")
  96. status = "running";
  97. else if (result?.status === "skipped")
  98. status = "skipped";
  99. else if (result?.status === "interrupted")
  100. status = "none";
  101. else if (result && test.outcome() !== "expected")
  102. status = "failed";
  103. else if (result && test.outcome() === "expected")
  104. status = "passed";
  105. testCaseItem.tests.push(test);
  106. const testItem = {
  107. kind: "test",
  108. id: test.id,
  109. title: project.name,
  110. location: test.location,
  111. test,
  112. parent: testCaseItem,
  113. children: [],
  114. status,
  115. duration: test.results.length ? Math.max(0, test.results[0].duration) : 0,
  116. project
  117. };
  118. this._addChild(testCaseItem, testItem);
  119. this._treeItemByTestId.set(test.id, testItem);
  120. testCaseItem.duration = testCaseItem.children.reduce((a, b) => a + b.duration, 0);
  121. }
  122. };
  123. for (const projectSuite of rootSuite?.suites || []) {
  124. if (filterProjects && !projectFilters.get(projectSuite.title))
  125. continue;
  126. for (const fileSuite of projectSuite.suites) {
  127. const fileItem = this._fileItem(fileSuite.location.file.split(pathSeparator), true);
  128. visitSuite(projectSuite.project(), fileSuite, fileItem);
  129. }
  130. }
  131. for (const loadError of loadErrors) {
  132. if (!loadError.location)
  133. continue;
  134. const fileItem = this._fileItem(loadError.location.file.split(pathSeparator), true);
  135. fileItem.hasLoadErrors = true;
  136. }
  137. }
  138. _addChild(parent, child) {
  139. parent.children.push(child);
  140. child.parent = parent;
  141. this._treeItemById.set(child.id, child);
  142. }
  143. filterTree(filterText, statusFilters, runningTestIds) {
  144. const tokens = filterText.trim().toLowerCase().split(" ");
  145. const filtersStatuses = [...statusFilters.values()].some(Boolean);
  146. const filter = (testCase) => {
  147. const titleWithTags = [...testCase.tests[0].titlePath(), ...testCase.tests[0].tags].join(" ").toLowerCase();
  148. if (!tokens.every((token) => titleWithTags.includes(token)) && !testCase.tests.some((t) => runningTestIds?.has(t.id)))
  149. return false;
  150. testCase.children = testCase.children.filter((test) => {
  151. return !filtersStatuses || runningTestIds?.has(test.test.id) || statusFilters.get(test.status);
  152. });
  153. testCase.tests = testCase.children.map((c) => c.test);
  154. return !!testCase.children.length;
  155. };
  156. const visit = (treeItem) => {
  157. const newChildren = [];
  158. for (const child of treeItem.children) {
  159. if (child.kind === "case") {
  160. if (filter(child))
  161. newChildren.push(child);
  162. } else {
  163. visit(child);
  164. if (child.children.length || child.hasLoadErrors)
  165. newChildren.push(child);
  166. }
  167. }
  168. treeItem.children = newChildren;
  169. };
  170. visit(this.rootItem);
  171. }
  172. _fileItem(filePath, isFile) {
  173. if (filePath.length === 0)
  174. return this.rootItem;
  175. const fileName = filePath.join(this.pathSeparator);
  176. const existingFileItem = this._treeItemById.get(fileName);
  177. if (existingFileItem)
  178. return existingFileItem;
  179. const parentFileItem = this._fileItem(filePath.slice(0, filePath.length - 1), false);
  180. const fileItem = {
  181. kind: "group",
  182. subKind: isFile ? "file" : "folder",
  183. id: fileName,
  184. title: filePath[filePath.length - 1],
  185. location: { file: fileName, line: 0, column: 0 },
  186. duration: 0,
  187. parent: parentFileItem,
  188. children: [],
  189. status: "none",
  190. hasLoadErrors: false
  191. };
  192. this._addChild(parentFileItem, fileItem);
  193. return fileItem;
  194. }
  195. sortAndPropagateStatus() {
  196. sortAndPropagateStatus(this.rootItem);
  197. }
  198. flattenForSingleProject() {
  199. const visit = (treeItem) => {
  200. if (treeItem.kind === "case" && treeItem.children.length === 1) {
  201. treeItem.project = treeItem.children[0].project;
  202. treeItem.test = treeItem.children[0].test;
  203. treeItem.children = [];
  204. this._treeItemByTestId.set(treeItem.test.id, treeItem);
  205. } else {
  206. treeItem.children.forEach(visit);
  207. }
  208. };
  209. visit(this.rootItem);
  210. }
  211. shortenRoot() {
  212. let shortRoot = this.rootItem;
  213. while (shortRoot.children.length === 1 && shortRoot.children[0].kind === "group" && shortRoot.children[0].subKind === "folder")
  214. shortRoot = shortRoot.children[0];
  215. shortRoot.location = this.rootItem.location;
  216. this.rootItem = shortRoot;
  217. }
  218. testIds() {
  219. const result = /* @__PURE__ */ new Set();
  220. const visit = (treeItem) => {
  221. if (treeItem.kind === "case")
  222. treeItem.tests.forEach((t) => result.add(t.id));
  223. treeItem.children.forEach(visit);
  224. };
  225. visit(this.rootItem);
  226. return result;
  227. }
  228. fileNames() {
  229. const result = /* @__PURE__ */ new Set();
  230. const visit = (treeItem) => {
  231. if (treeItem.kind === "group" && treeItem.subKind === "file")
  232. result.add(treeItem.id);
  233. else
  234. treeItem.children.forEach(visit);
  235. };
  236. visit(this.rootItem);
  237. return [...result];
  238. }
  239. flatTreeItems() {
  240. const result = [];
  241. const visit = (treeItem) => {
  242. result.push(treeItem);
  243. treeItem.children.forEach(visit);
  244. };
  245. visit(this.rootItem);
  246. return result;
  247. }
  248. treeItemById(id) {
  249. return this._treeItemById.get(id);
  250. }
  251. collectTestIds(treeItem) {
  252. return treeItem ? collectTestIds(treeItem) : /* @__PURE__ */ new Set();
  253. }
  254. }
  255. function sortAndPropagateStatus(treeItem) {
  256. for (const child of treeItem.children)
  257. sortAndPropagateStatus(child);
  258. if (treeItem.kind === "group") {
  259. treeItem.children.sort((a, b) => {
  260. const fc = a.location.file.localeCompare(b.location.file);
  261. return fc || a.location.line - b.location.line;
  262. });
  263. }
  264. let allPassed = treeItem.children.length > 0;
  265. let allSkipped = treeItem.children.length > 0;
  266. let hasFailed = false;
  267. let hasRunning = false;
  268. let hasScheduled = false;
  269. for (const child of treeItem.children) {
  270. allSkipped = allSkipped && child.status === "skipped";
  271. allPassed = allPassed && (child.status === "passed" || child.status === "skipped");
  272. hasFailed = hasFailed || child.status === "failed";
  273. hasRunning = hasRunning || child.status === "running";
  274. hasScheduled = hasScheduled || child.status === "scheduled";
  275. }
  276. if (hasRunning)
  277. treeItem.status = "running";
  278. else if (hasScheduled)
  279. treeItem.status = "scheduled";
  280. else if (hasFailed)
  281. treeItem.status = "failed";
  282. else if (allSkipped)
  283. treeItem.status = "skipped";
  284. else if (allPassed)
  285. treeItem.status = "passed";
  286. }
  287. function collectTestIds(treeItem) {
  288. const testIds = /* @__PURE__ */ new Set();
  289. const visit = (treeItem2) => {
  290. if (treeItem2.kind === "case")
  291. treeItem2.tests.map((t) => t.id).forEach((id) => testIds.add(id));
  292. else if (treeItem2.kind === "test")
  293. testIds.add(treeItem2.id);
  294. else
  295. treeItem2.children?.forEach(visit);
  296. };
  297. visit(treeItem);
  298. return testIds;
  299. }
  300. const statusEx = Symbol("statusEx");
  301. // Annotate the CommonJS export names for ESM import in node:
  302. 0 && (module.exports = {
  303. TestTree,
  304. collectTestIds,
  305. sortAndPropagateStatus,
  306. statusEx
  307. });