html.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var html_exports = {};
  30. __export(html_exports, {
  31. default: () => html_default,
  32. showHTMLReport: () => showHTMLReport,
  33. startHtmlReportServer: () => startHtmlReportServer
  34. });
  35. module.exports = __toCommonJS(html_exports);
  36. var import_fs = __toESM(require("fs"));
  37. var import_path = __toESM(require("path"));
  38. var import_stream = require("stream");
  39. var import_utils = require("playwright-core/lib/utils");
  40. var import_utils2 = require("playwright-core/lib/utils");
  41. var import_utilsBundle = require("playwright-core/lib/utilsBundle");
  42. var import_utilsBundle2 = require("playwright-core/lib/utilsBundle");
  43. var import_zipBundle = require("playwright-core/lib/zipBundle");
  44. var import_base = require("./base");
  45. var import_babelBundle = require("../transform/babelBundle");
  46. var import_util = require("../util");
  47. const htmlReportOptions = ["always", "never", "on-failure"];
  48. const isHtmlReportOption = (type) => {
  49. return htmlReportOptions.includes(type);
  50. };
  51. class HtmlReporter {
  52. constructor(options) {
  53. this._topLevelErrors = [];
  54. this._options = options;
  55. }
  56. version() {
  57. return "v2";
  58. }
  59. printsToStdio() {
  60. return false;
  61. }
  62. onConfigure(config) {
  63. this.config = config;
  64. }
  65. onBegin(suite) {
  66. const { outputFolder, open: open2, attachmentsBaseURL, host, port } = this._resolveOptions();
  67. this._outputFolder = outputFolder;
  68. this._open = open2;
  69. this._host = host;
  70. this._port = port;
  71. this._attachmentsBaseURL = attachmentsBaseURL;
  72. const reportedWarnings = /* @__PURE__ */ new Set();
  73. for (const project of this.config.projects) {
  74. if (this._isSubdirectory(outputFolder, project.outputDir) || this._isSubdirectory(project.outputDir, outputFolder)) {
  75. const key = outputFolder + "|" + project.outputDir;
  76. if (reportedWarnings.has(key))
  77. continue;
  78. reportedWarnings.add(key);
  79. writeLine(import_utils2.colors.red(`Configuration Error: HTML reporter output folder clashes with the tests output folder:`));
  80. writeLine(`
  81. html reporter folder: ${import_utils2.colors.bold(outputFolder)}
  82. test results folder: ${import_utils2.colors.bold(project.outputDir)}`);
  83. writeLine("");
  84. writeLine(`HTML reporter will clear its output directory prior to being generated, which will lead to the artifact loss.
  85. `);
  86. }
  87. }
  88. this.suite = suite;
  89. }
  90. _resolveOptions() {
  91. const outputFolder = reportFolderFromEnv() ?? (0, import_util.resolveReporterOutputPath)("playwright-report", this._options.configDir, this._options.outputFolder);
  92. return {
  93. outputFolder,
  94. open: getHtmlReportOptionProcessEnv() || this._options.open || "on-failure",
  95. attachmentsBaseURL: process.env.PLAYWRIGHT_HTML_ATTACHMENTS_BASE_URL || this._options.attachmentsBaseURL || "data/",
  96. host: process.env.PLAYWRIGHT_HTML_HOST || this._options.host,
  97. port: process.env.PLAYWRIGHT_HTML_PORT ? +process.env.PLAYWRIGHT_HTML_PORT : this._options.port
  98. };
  99. }
  100. _isSubdirectory(parentDir, dir) {
  101. const relativePath = import_path.default.relative(parentDir, dir);
  102. return !!relativePath && !relativePath.startsWith("..") && !import_path.default.isAbsolute(relativePath);
  103. }
  104. onError(error) {
  105. this._topLevelErrors.push(error);
  106. }
  107. async onEnd(result) {
  108. const projectSuites = this.suite.suites;
  109. await (0, import_utils.removeFolders)([this._outputFolder]);
  110. let noSnippets;
  111. if (process.env.PLAYWRIGHT_HTML_NO_SNIPPETS === "false" || process.env.PLAYWRIGHT_HTML_NO_SNIPPETS === "0")
  112. noSnippets = false;
  113. else if (process.env.PLAYWRIGHT_HTML_NO_SNIPPETS)
  114. noSnippets = true;
  115. noSnippets = noSnippets || this._options.noSnippets;
  116. const builder = new HtmlBuilder(this.config, this._outputFolder, this._attachmentsBaseURL, process.env.PLAYWRIGHT_HTML_TITLE || this._options.title, noSnippets);
  117. this._buildResult = await builder.build(this.config.metadata, projectSuites, result, this._topLevelErrors);
  118. }
  119. async onExit() {
  120. if (process.env.CI || !this._buildResult)
  121. return;
  122. const { ok, singleTestId } = this._buildResult;
  123. const shouldOpen = !this._options._isTestServer && (this._open === "always" || !ok && this._open === "on-failure");
  124. if (shouldOpen) {
  125. await showHTMLReport(this._outputFolder, this._host, this._port, singleTestId);
  126. } else if (this._options._mode === "test" && !this._options._isTestServer) {
  127. const packageManagerCommand = (0, import_utils.getPackageManagerExecCommand)();
  128. const relativeReportPath = this._outputFolder === standaloneDefaultFolder() ? "" : " " + import_path.default.relative(process.cwd(), this._outputFolder);
  129. const hostArg = this._host ? ` --host ${this._host}` : "";
  130. const portArg = this._port ? ` --port ${this._port}` : "";
  131. writeLine("");
  132. writeLine("To open last HTML report run:");
  133. writeLine(import_utils2.colors.cyan(`
  134. ${packageManagerCommand} playwright show-report${relativeReportPath}${hostArg}${portArg}
  135. `));
  136. }
  137. }
  138. }
  139. function reportFolderFromEnv() {
  140. const envValue = process.env.PLAYWRIGHT_HTML_OUTPUT_DIR || process.env.PLAYWRIGHT_HTML_REPORT;
  141. return envValue ? import_path.default.resolve(envValue) : void 0;
  142. }
  143. function getHtmlReportOptionProcessEnv() {
  144. const htmlOpenEnv = process.env.PLAYWRIGHT_HTML_OPEN || process.env.PW_TEST_HTML_REPORT_OPEN;
  145. if (!htmlOpenEnv)
  146. return void 0;
  147. if (!isHtmlReportOption(htmlOpenEnv)) {
  148. writeLine(import_utils2.colors.red(`Configuration Error: HTML reporter Invalid value for PLAYWRIGHT_HTML_OPEN: ${htmlOpenEnv}. Valid values are: ${htmlReportOptions.join(", ")}`));
  149. return void 0;
  150. }
  151. return htmlOpenEnv;
  152. }
  153. function standaloneDefaultFolder() {
  154. return reportFolderFromEnv() ?? (0, import_util.resolveReporterOutputPath)("playwright-report", process.cwd(), void 0);
  155. }
  156. async function showHTMLReport(reportFolder, host = "localhost", port, testId) {
  157. const folder = reportFolder ?? standaloneDefaultFolder();
  158. try {
  159. (0, import_utils.assert)(import_fs.default.statSync(folder).isDirectory());
  160. } catch (e) {
  161. writeLine(import_utils2.colors.red(`No report found at "${folder}"`));
  162. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  163. return;
  164. }
  165. const server = startHtmlReportServer(folder);
  166. await server.start({ port, host, preferredPort: port ? void 0 : 9323 });
  167. let url = server.urlPrefix("human-readable");
  168. writeLine("");
  169. writeLine(import_utils2.colors.cyan(` Serving HTML report at ${url}. Press Ctrl+C to quit.`));
  170. if (testId)
  171. url += `#?testId=${testId}`;
  172. url = url.replace("0.0.0.0", "localhost");
  173. await (0, import_utilsBundle.open)(url, { wait: true }).catch(() => {
  174. });
  175. await new Promise(() => {
  176. });
  177. }
  178. function startHtmlReportServer(folder) {
  179. const server = new import_utils.HttpServer();
  180. server.routePrefix("/", (request, response) => {
  181. let relativePath = new URL("http://localhost" + request.url).pathname;
  182. if (relativePath.startsWith("/trace/file")) {
  183. const url = new URL("http://localhost" + request.url);
  184. try {
  185. return server.serveFile(request, response, url.searchParams.get("path"));
  186. } catch (e) {
  187. return false;
  188. }
  189. }
  190. if (relativePath.endsWith("/stall.js"))
  191. return true;
  192. if (relativePath === "/")
  193. relativePath = "/index.html";
  194. const absolutePath = import_path.default.join(folder, ...relativePath.split("/"));
  195. return server.serveFile(request, response, absolutePath);
  196. });
  197. return server;
  198. }
  199. class HtmlBuilder {
  200. constructor(config, outputDir, attachmentsBaseURL, title, noSnippets = false) {
  201. this._stepsInFile = new import_utils.MultiMap();
  202. this._hasTraces = false;
  203. this._config = config;
  204. this._reportFolder = outputDir;
  205. this._noSnippets = noSnippets;
  206. import_fs.default.mkdirSync(this._reportFolder, { recursive: true });
  207. this._dataZipFile = new import_zipBundle.yazl.ZipFile();
  208. this._attachmentsBaseURL = attachmentsBaseURL;
  209. this._title = title;
  210. }
  211. async build(metadata, projectSuites, result, topLevelErrors) {
  212. const data = /* @__PURE__ */ new Map();
  213. for (const projectSuite of projectSuites) {
  214. for (const fileSuite of projectSuite.suites) {
  215. const fileName = this._relativeLocation(fileSuite.location).file;
  216. const fileId = (0, import_utils.calculateSha1)((0, import_utils.toPosixPath)(fileName)).slice(0, 20);
  217. let fileEntry = data.get(fileId);
  218. if (!fileEntry) {
  219. fileEntry = {
  220. testFile: { fileId, fileName, tests: [] },
  221. testFileSummary: { fileId, fileName, tests: [], stats: emptyStats() }
  222. };
  223. data.set(fileId, fileEntry);
  224. }
  225. const { testFile, testFileSummary } = fileEntry;
  226. const testEntries = [];
  227. this._processSuite(fileSuite, projectSuite.project().name, [], testEntries);
  228. for (const test of testEntries) {
  229. testFile.tests.push(test.testCase);
  230. testFileSummary.tests.push(test.testCaseSummary);
  231. }
  232. }
  233. }
  234. if (!this._noSnippets)
  235. createSnippets(this._stepsInFile);
  236. let ok = true;
  237. for (const [fileId, { testFile, testFileSummary }] of data) {
  238. const stats = testFileSummary.stats;
  239. for (const test of testFileSummary.tests) {
  240. if (test.outcome === "expected")
  241. ++stats.expected;
  242. if (test.outcome === "skipped")
  243. ++stats.skipped;
  244. if (test.outcome === "unexpected")
  245. ++stats.unexpected;
  246. if (test.outcome === "flaky")
  247. ++stats.flaky;
  248. ++stats.total;
  249. }
  250. stats.ok = stats.unexpected + stats.flaky === 0;
  251. if (!stats.ok)
  252. ok = false;
  253. const testCaseSummaryComparator = (t1, t2) => {
  254. const w1 = (t1.outcome === "unexpected" ? 1e3 : 0) + (t1.outcome === "flaky" ? 1 : 0);
  255. const w2 = (t2.outcome === "unexpected" ? 1e3 : 0) + (t2.outcome === "flaky" ? 1 : 0);
  256. return w2 - w1;
  257. };
  258. testFileSummary.tests.sort(testCaseSummaryComparator);
  259. this._addDataFile(fileId + ".json", testFile);
  260. }
  261. const htmlReport = {
  262. metadata,
  263. title: this._title,
  264. startTime: result.startTime.getTime(),
  265. duration: result.duration,
  266. files: [...data.values()].map((e) => e.testFileSummary),
  267. projectNames: projectSuites.map((r) => r.project().name),
  268. stats: { ...[...data.values()].reduce((a, e) => addStats(a, e.testFileSummary.stats), emptyStats()) },
  269. errors: topLevelErrors.map((error) => (0, import_base.formatError)(import_base.internalScreen, error).message)
  270. };
  271. htmlReport.files.sort((f1, f2) => {
  272. const w1 = f1.stats.unexpected * 1e3 + f1.stats.flaky;
  273. const w2 = f2.stats.unexpected * 1e3 + f2.stats.flaky;
  274. return w2 - w1;
  275. });
  276. this._addDataFile("report.json", htmlReport);
  277. let singleTestId;
  278. if (htmlReport.stats.total === 1) {
  279. const testFile = data.values().next().value.testFile;
  280. singleTestId = testFile.tests[0].testId;
  281. }
  282. if (process.env.PW_HMR === "1") {
  283. const redirectFile = import_path.default.join(this._reportFolder, "index.html");
  284. await this._writeReportData(redirectFile);
  285. async function redirect() {
  286. const hmrURL = new URL("http://localhost:44224");
  287. const popup = window.open(hmrURL);
  288. const listener = (evt) => {
  289. if (evt.source === popup && evt.data === "ready") {
  290. const element = document.getElementById("playwrightReportBase64");
  291. popup.postMessage(element?.textContent ?? "", hmrURL.origin);
  292. window.removeEventListener("message", listener);
  293. window.close();
  294. }
  295. };
  296. window.addEventListener("message", listener);
  297. }
  298. import_fs.default.appendFileSync(redirectFile, `<script>(${redirect.toString()})()</script>`);
  299. return { ok, singleTestId };
  300. }
  301. const appFolder = import_path.default.join(require.resolve("playwright-core"), "..", "lib", "vite", "htmlReport");
  302. await (0, import_utils.copyFileAndMakeWritable)(import_path.default.join(appFolder, "index.html"), import_path.default.join(this._reportFolder, "index.html"));
  303. if (this._hasTraces) {
  304. const traceViewerFolder = import_path.default.join(require.resolve("playwright-core"), "..", "lib", "vite", "traceViewer");
  305. const traceViewerTargetFolder = import_path.default.join(this._reportFolder, "trace");
  306. const traceViewerAssetsTargetFolder = import_path.default.join(traceViewerTargetFolder, "assets");
  307. import_fs.default.mkdirSync(traceViewerAssetsTargetFolder, { recursive: true });
  308. for (const file of import_fs.default.readdirSync(traceViewerFolder)) {
  309. if (file.endsWith(".map") || file.includes("watch") || file.includes("assets"))
  310. continue;
  311. await (0, import_utils.copyFileAndMakeWritable)(import_path.default.join(traceViewerFolder, file), import_path.default.join(traceViewerTargetFolder, file));
  312. }
  313. for (const file of import_fs.default.readdirSync(import_path.default.join(traceViewerFolder, "assets"))) {
  314. if (file.endsWith(".map") || file.includes("xtermModule"))
  315. continue;
  316. await (0, import_utils.copyFileAndMakeWritable)(import_path.default.join(traceViewerFolder, "assets", file), import_path.default.join(traceViewerAssetsTargetFolder, file));
  317. }
  318. }
  319. await this._writeReportData(import_path.default.join(this._reportFolder, "index.html"));
  320. return { ok, singleTestId };
  321. }
  322. async _writeReportData(filePath) {
  323. import_fs.default.appendFileSync(filePath, '<script id="playwrightReportBase64" type="application/zip">data:application/zip;base64,');
  324. await new Promise((f) => {
  325. this._dataZipFile.end(void 0, () => {
  326. this._dataZipFile.outputStream.pipe(new Base64Encoder()).pipe(import_fs.default.createWriteStream(filePath, { flags: "a" })).on("close", f);
  327. });
  328. });
  329. import_fs.default.appendFileSync(filePath, "</script>");
  330. }
  331. _addDataFile(fileName, data) {
  332. this._dataZipFile.addBuffer(Buffer.from(JSON.stringify(data)), fileName);
  333. }
  334. _processSuite(suite, projectName, path2, outTests) {
  335. const newPath = [...path2, suite.title];
  336. suite.entries().forEach((e) => {
  337. if (e.type === "test")
  338. outTests.push(this._createTestEntry(e, projectName, newPath));
  339. else
  340. this._processSuite(e, projectName, newPath, outTests);
  341. });
  342. }
  343. _createTestEntry(test, projectName, path2) {
  344. const duration = test.results.reduce((a, r) => a + r.duration, 0);
  345. const location = this._relativeLocation(test.location);
  346. path2 = path2.slice(1).filter((path3) => path3.length > 0);
  347. const results = test.results.map((r) => this._createTestResult(test, r));
  348. return {
  349. testCase: {
  350. testId: test.id,
  351. title: test.title,
  352. projectName,
  353. location,
  354. duration,
  355. annotations: this._serializeAnnotations(test.annotations),
  356. tags: test.tags,
  357. outcome: test.outcome(),
  358. path: path2,
  359. results,
  360. ok: test.outcome() === "expected" || test.outcome() === "flaky"
  361. },
  362. testCaseSummary: {
  363. testId: test.id,
  364. title: test.title,
  365. projectName,
  366. location,
  367. duration,
  368. annotations: this._serializeAnnotations(test.annotations),
  369. tags: test.tags,
  370. outcome: test.outcome(),
  371. path: path2,
  372. ok: test.outcome() === "expected" || test.outcome() === "flaky",
  373. results: results.map((result) => {
  374. return { attachments: result.attachments.map((a) => ({ name: a.name, contentType: a.contentType, path: a.path })) };
  375. })
  376. }
  377. };
  378. }
  379. _serializeAttachments(attachments) {
  380. let lastAttachment;
  381. return attachments.map((a) => {
  382. if (a.name === "trace")
  383. this._hasTraces = true;
  384. if ((a.name === "stdout" || a.name === "stderr") && a.contentType === "text/plain") {
  385. if (lastAttachment && lastAttachment.name === a.name && lastAttachment.contentType === a.contentType) {
  386. lastAttachment.body += (0, import_util.stripAnsiEscapes)(a.body);
  387. return null;
  388. }
  389. a.body = (0, import_util.stripAnsiEscapes)(a.body);
  390. lastAttachment = a;
  391. return a;
  392. }
  393. if (a.path) {
  394. let fileName = a.path;
  395. try {
  396. const buffer = import_fs.default.readFileSync(a.path);
  397. const sha1 = (0, import_utils.calculateSha1)(buffer) + import_path.default.extname(a.path);
  398. fileName = this._attachmentsBaseURL + sha1;
  399. import_fs.default.mkdirSync(import_path.default.join(this._reportFolder, "data"), { recursive: true });
  400. import_fs.default.writeFileSync(import_path.default.join(this._reportFolder, "data", sha1), buffer);
  401. } catch (e) {
  402. }
  403. return {
  404. name: a.name,
  405. contentType: a.contentType,
  406. path: fileName,
  407. body: a.body
  408. };
  409. }
  410. if (a.body instanceof Buffer) {
  411. if (isTextContentType(a.contentType)) {
  412. const charset = a.contentType.match(/charset=(.*)/)?.[1];
  413. try {
  414. const body = a.body.toString(charset || "utf-8");
  415. return {
  416. name: a.name,
  417. contentType: a.contentType,
  418. body
  419. };
  420. } catch (e) {
  421. }
  422. }
  423. import_fs.default.mkdirSync(import_path.default.join(this._reportFolder, "data"), { recursive: true });
  424. const extension = (0, import_utils.sanitizeForFilePath)(import_path.default.extname(a.name).replace(/^\./, "")) || import_utilsBundle2.mime.getExtension(a.contentType) || "dat";
  425. const sha1 = (0, import_utils.calculateSha1)(a.body) + "." + extension;
  426. import_fs.default.writeFileSync(import_path.default.join(this._reportFolder, "data", sha1), a.body);
  427. return {
  428. name: a.name,
  429. contentType: a.contentType,
  430. path: this._attachmentsBaseURL + sha1
  431. };
  432. }
  433. return {
  434. name: a.name,
  435. contentType: a.contentType,
  436. body: a.body
  437. };
  438. }).filter(Boolean);
  439. }
  440. _serializeAnnotations(annotations) {
  441. return annotations.map((a) => ({
  442. type: a.type,
  443. description: a.description === void 0 ? void 0 : String(a.description),
  444. location: a.location ? {
  445. file: a.location.file,
  446. line: a.location.line,
  447. column: a.location.column
  448. } : void 0
  449. }));
  450. }
  451. _createTestResult(test, result) {
  452. return {
  453. duration: result.duration,
  454. startTime: result.startTime.toISOString(),
  455. retry: result.retry,
  456. steps: dedupeSteps(result.steps).map((s) => this._createTestStep(s, result)),
  457. errors: (0, import_base.formatResultFailure)(import_base.internalScreen, test, result, "").map((error) => {
  458. return {
  459. message: error.message,
  460. codeframe: error.location ? createErrorCodeframe(error.message, error.location) : void 0
  461. };
  462. }),
  463. status: result.status,
  464. annotations: this._serializeAnnotations(result.annotations),
  465. attachments: this._serializeAttachments([
  466. ...result.attachments,
  467. ...result.stdout.map((m) => stdioAttachment(m, "stdout")),
  468. ...result.stderr.map((m) => stdioAttachment(m, "stderr"))
  469. ])
  470. };
  471. }
  472. _createTestStep(dedupedStep, result) {
  473. const { step, duration, count } = dedupedStep;
  474. const skipped = dedupedStep.step.annotations?.find((a) => a.type === "skip");
  475. let title = step.title;
  476. if (skipped)
  477. title = `${title} (skipped${skipped.description ? ": " + skipped.description : ""})`;
  478. const testStep = {
  479. title,
  480. startTime: step.startTime.toISOString(),
  481. duration,
  482. steps: dedupeSteps(step.steps).map((s) => this._createTestStep(s, result)),
  483. attachments: step.attachments.map((s) => {
  484. const index = result.attachments.indexOf(s);
  485. if (index === -1)
  486. throw new Error("Unexpected, attachment not found");
  487. return index;
  488. }),
  489. location: this._relativeLocation(step.location),
  490. error: step.error?.message,
  491. count,
  492. skipped: !!skipped
  493. };
  494. if (step.location)
  495. this._stepsInFile.set(step.location.file, testStep);
  496. return testStep;
  497. }
  498. _relativeLocation(location) {
  499. if (!location)
  500. return void 0;
  501. const file = (0, import_utils.toPosixPath)(import_path.default.relative(this._config.rootDir, location.file));
  502. return {
  503. file,
  504. line: location.line,
  505. column: location.column
  506. };
  507. }
  508. }
  509. const emptyStats = () => {
  510. return {
  511. total: 0,
  512. expected: 0,
  513. unexpected: 0,
  514. flaky: 0,
  515. skipped: 0,
  516. ok: true
  517. };
  518. };
  519. const addStats = (stats, delta) => {
  520. stats.total += delta.total;
  521. stats.skipped += delta.skipped;
  522. stats.expected += delta.expected;
  523. stats.unexpected += delta.unexpected;
  524. stats.flaky += delta.flaky;
  525. stats.ok = stats.ok && delta.ok;
  526. return stats;
  527. };
  528. class Base64Encoder extends import_stream.Transform {
  529. _transform(chunk, encoding, callback) {
  530. if (this._remainder) {
  531. chunk = Buffer.concat([this._remainder, chunk]);
  532. this._remainder = void 0;
  533. }
  534. const remaining = chunk.length % 3;
  535. if (remaining) {
  536. this._remainder = chunk.slice(chunk.length - remaining);
  537. chunk = chunk.slice(0, chunk.length - remaining);
  538. }
  539. chunk = chunk.toString("base64");
  540. this.push(Buffer.from(chunk));
  541. callback();
  542. }
  543. _flush(callback) {
  544. if (this._remainder)
  545. this.push(Buffer.from(this._remainder.toString("base64")));
  546. callback();
  547. }
  548. }
  549. function isTextContentType(contentType) {
  550. return contentType.startsWith("text/") || contentType.startsWith("application/json");
  551. }
  552. function stdioAttachment(chunk, type) {
  553. return {
  554. name: type,
  555. contentType: "text/plain",
  556. body: typeof chunk === "string" ? chunk : chunk.toString("utf-8")
  557. };
  558. }
  559. function dedupeSteps(steps) {
  560. const result = [];
  561. let lastResult = void 0;
  562. for (const step of steps) {
  563. const canDedupe = !step.error && step.duration >= 0 && step.location?.file && !step.steps.length;
  564. const lastStep = lastResult?.step;
  565. if (canDedupe && lastResult && lastStep && step.category === lastStep.category && step.title === lastStep.title && step.location?.file === lastStep.location?.file && step.location?.line === lastStep.location?.line && step.location?.column === lastStep.location?.column) {
  566. ++lastResult.count;
  567. lastResult.duration += step.duration;
  568. continue;
  569. }
  570. lastResult = { step, count: 1, duration: step.duration };
  571. result.push(lastResult);
  572. if (!canDedupe)
  573. lastResult = void 0;
  574. }
  575. return result;
  576. }
  577. function createSnippets(stepsInFile) {
  578. for (const file of stepsInFile.keys()) {
  579. let source;
  580. try {
  581. source = import_fs.default.readFileSync(file, "utf-8") + "\n//";
  582. } catch (e) {
  583. continue;
  584. }
  585. const lines = source.split("\n").length;
  586. const highlighted = (0, import_babelBundle.codeFrameColumns)(source, { start: { line: lines, column: 1 } }, { highlightCode: true, linesAbove: lines, linesBelow: 0 });
  587. const highlightedLines = highlighted.split("\n");
  588. const lineWithArrow = highlightedLines[highlightedLines.length - 1];
  589. for (const step of stepsInFile.get(file)) {
  590. if (step.location.line < 2 || step.location.line >= lines)
  591. continue;
  592. const snippetLines = highlightedLines.slice(step.location.line - 2, step.location.line + 1);
  593. const index = lineWithArrow.indexOf("^");
  594. const shiftedArrow = lineWithArrow.slice(0, index) + " ".repeat(step.location.column - 1) + lineWithArrow.slice(index);
  595. snippetLines.splice(2, 0, shiftedArrow);
  596. step.snippet = snippetLines.join("\n");
  597. }
  598. }
  599. }
  600. function createErrorCodeframe(message, location) {
  601. let source;
  602. try {
  603. source = import_fs.default.readFileSync(location.file, "utf-8") + "\n//";
  604. } catch (e) {
  605. return;
  606. }
  607. return (0, import_babelBundle.codeFrameColumns)(
  608. source,
  609. {
  610. start: {
  611. line: location.line,
  612. column: location.column
  613. }
  614. },
  615. {
  616. highlightCode: false,
  617. linesAbove: 100,
  618. linesBelow: 100,
  619. message: (0, import_util.stripAnsiEscapes)(message).split("\n")[0] || void 0
  620. }
  621. );
  622. }
  623. function writeLine(line) {
  624. process.stdout.write(line + "\n");
  625. }
  626. var html_default = HtmlReporter;
  627. // Annotate the CommonJS export names for ESM import in node:
  628. 0 && (module.exports = {
  629. showHTMLReport,
  630. startHtmlReportServer
  631. });