e2eReportingSummarySkipped.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { mkdtempSync, readFileSync, rmSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { createE2EReporter, resolveWdioTestStatus } from "./e2e/reporting/e2e-reporting.js";
  6. import { appendJsonLine, writeE2ESummary } from "./e2e/reporting/e2e-reporting-summary.js";
  7. import type { TestResultLine } from "./e2e/reporting/e2e-reporting-types.js";
  8. const tempDirs: string[] = [];
  9. function createTempDir() {
  10. const dir = mkdtempSync(join(tmpdir(), "e2e-summary-skipped-"));
  11. tempDirs.push(dir);
  12. return dir;
  13. }
  14. describe("e2e reporting summary skipped tests", () => {
  15. afterEach(() => {
  16. for (const dir of tempDirs.splice(0)) {
  17. rmSync(dir, { recursive: true, force: true });
  18. }
  19. });
  20. it("does not count WDIO skipped tests as failures", () => {
  21. const artifactDir = createTempDir();
  22. const testsPath = join(artifactDir, "test-results.ndjson");
  23. const summaryJsonPath = join(artifactDir, "summary.json");
  24. const summaryMdPath = join(artifactDir, "summary.md");
  25. appendJsonLine(testsPath, {
  26. capturedAt: "2026-07-09T00:00:00.000Z",
  27. durationMs: 0,
  28. fullTitle: "file tools POSIX executable bit",
  29. passed: false,
  30. status: "skipped",
  31. title: "file tools POSIX executable bit",
  32. } satisfies TestResultLine);
  33. appendJsonLine(testsPath, {
  34. capturedAt: "2026-07-09T00:00:01.000Z",
  35. durationMs: 100,
  36. fullTitle: "actual failing case",
  37. passed: false,
  38. status: "failed",
  39. title: "actual failing case",
  40. errorCategory: "assertion",
  41. } satisfies TestResultLine);
  42. appendJsonLine(testsPath, {
  43. capturedAt: "2026-07-09T00:00:02.000Z",
  44. durationMs: 50,
  45. fullTitle: "passing case",
  46. passed: true,
  47. status: "passed",
  48. title: "passing case",
  49. } satisfies TestResultLine);
  50. writeE2ESummary({
  51. artifactDir,
  52. containerE2E: false,
  53. containerSamplesPath: join(artifactDir, "container-samples.ndjson"),
  54. exitCode: 1,
  55. manifestPath: join(artifactDir, "manifest.json"),
  56. perfDir: join(artifactDir, "perf"),
  57. requestedSpecs: [],
  58. runId: "skipped-summary",
  59. startedAt: new Date("2026-07-09T00:00:00.000Z"),
  60. summaryJsonPath,
  61. summaryMdPath,
  62. testsPath,
  63. });
  64. const summary = JSON.parse(readFileSync(summaryJsonPath, "utf-8"));
  65. const summaryMd = readFileSync(summaryMdPath, "utf-8");
  66. expect(summary.tests.total).toBe(3);
  67. expect(summary.tests.passed).toBe(1);
  68. expect(summary.tests.failed).toBe(1);
  69. expect(summary.tests.skipped).toBe(1);
  70. expect(summary.stability.passRate).toBe(0.5);
  71. expect(summary.failureBreakdown.assertion).toBe(1);
  72. expect(summary.failureBreakdown.unknown).toBe(0);
  73. expect(summary.timing.caseDurationP50Ms).toBe(50);
  74. expect(summaryMd).toContain("- Skipped: `1`");
  75. expect(summaryMd).toContain("| SKIP | 0ms | file tools POSIX executable bit |");
  76. });
  77. it("treats legacy skipped rows without status as skipped", () => {
  78. const artifactDir = createTempDir();
  79. const testsPath = join(artifactDir, "test-results.ndjson");
  80. const summaryJsonPath = join(artifactDir, "summary.json");
  81. const summaryMdPath = join(artifactDir, "summary.md");
  82. appendJsonLine(testsPath, {
  83. capturedAt: "2026-07-09T00:00:00.000Z",
  84. durationMs: 0,
  85. errorCategory: "unknown",
  86. fullTitle: "Write 和 Edit 修改可执行脚本后应保留 POSIX 执行位",
  87. passed: false,
  88. title: "Write 和 Edit 修改可执行脚本后应保留 POSIX 执行位",
  89. });
  90. writeE2ESummary({
  91. artifactDir,
  92. containerE2E: false,
  93. containerSamplesPath: join(artifactDir, "container-samples.ndjson"),
  94. exitCode: 0,
  95. manifestPath: join(artifactDir, "manifest.json"),
  96. perfDir: join(artifactDir, "perf"),
  97. requestedSpecs: [],
  98. runId: "legacy-skipped-summary",
  99. startedAt: new Date("2026-07-09T00:00:00.000Z"),
  100. summaryJsonPath,
  101. summaryMdPath,
  102. testsPath,
  103. });
  104. const summary = JSON.parse(readFileSync(summaryJsonPath, "utf-8"));
  105. expect(summary.tests.total).toBe(1);
  106. expect(summary.tests.failed).toBe(0);
  107. expect(summary.tests.skipped).toBe(1);
  108. expect(summary.failureBreakdown.unknown).toBe(0);
  109. });
  110. it("resolves WDIO 9 afterTest results, which mark this.skip() only with skipped=true", () => {
  111. expect(resolveWdioTestStatus({ passed: false, skipped: true })).toBe("skipped");
  112. expect(resolveWdioTestStatus({ passed: true, skipped: false })).toBe("passed");
  113. expect(resolveWdioTestStatus({ passed: false, skipped: false })).toBe("failed");
  114. expect(resolveWdioTestStatus({ passed: false, status: "pending" })).toBe("pending");
  115. });
  116. it("writes skipped per-case rows that downstream runners can read without guessing", () => {
  117. const artifactDir = createTempDir();
  118. const previous = process.env.ZCODE_E2E_ARTIFACT_DIR;
  119. process.env.ZCODE_E2E_ARTIFACT_DIR = artifactDir;
  120. try {
  121. const reporter = createE2EReporter({
  122. containerE2E: false,
  123. desktopDir: artifactDir,
  124. repoRoot: process.cwd(),
  125. requestedSpecs: [],
  126. });
  127. // WDIO 9 对 Mocha this.skip() 传给 afterTest 的真实形状:没有 status,只有 skipped。
  128. reporter.recordTestResult(
  129. { title: "windows only case" },
  130. { duration: 1, passed: false, skipped: true },
  131. );
  132. reporter.recordTestResult(
  133. { title: "real failure" },
  134. { duration: 5, error: new Error("expected 1 to be 2"), passed: false, skipped: false },
  135. );
  136. const rows = readFileSync(join(artifactDir, "test-results.ndjson"), "utf-8")
  137. .trim()
  138. .split("\n")
  139. .map((line) => JSON.parse(line));
  140. expect(rows[0]).toMatchObject({ passed: false, status: "skipped" });
  141. expect(rows[0].errorCategory).toBeUndefined();
  142. expect(rows[1]).toMatchObject({ passed: false, status: "failed" });
  143. expect(rows[1].errorCategory).toBeDefined();
  144. } finally {
  145. if (previous === undefined) delete process.env.ZCODE_E2E_ARTIFACT_DIR;
  146. else process.env.ZCODE_E2E_ARTIFACT_DIR = previous;
  147. }
  148. });
  149. });