@bigbinary/neeto-playwright-reporter 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs.js CHANGED
@@ -12,6 +12,7 @@ var require$$1$2 = require('tty');
12
12
  var require$$0$2 = require('os');
13
13
  var zlib = require('zlib');
14
14
  var EventEmitter = require('events');
15
+ var childProcess = require('child_process');
15
16
 
16
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
18
 
@@ -27,6 +28,7 @@ var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
27
28
  var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
28
29
  var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
29
30
  var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
31
+ var childProcess__default = /*#__PURE__*/_interopDefaultLegacy(childProcess);
30
32
 
31
33
  function bind(fn, thisArg) {
32
34
  return function wrap() {
@@ -18446,54 +18448,194 @@ axios.HttpStatusCode = HttpStatusCode;
18446
18448
 
18447
18449
  axios.default = axios;
18448
18450
 
18449
- // dummy endpoint until neeto-playwright-reporter dashboard is up
18450
- const create = (payload) => axios.post(`https://webhook.site/1bff83d0-03a0-412f-a70a-2f11f3209534`, payload);
18451
- const dummyApi = { create };
18452
-
18453
- const getDescribePath = ({ titlePath, title, project, spec, }) => {
18454
- const describePaths = titlePath.filter((item, index) => index !== 0 && item !== title && item !== project && item !== spec);
18455
- return describePaths.join(" > ");
18456
- };
18457
-
18458
18451
  const HEADERS_KEYS = {
18459
18452
  applicationKey: "Application-Key",
18460
- xCsrfToken: "X-CSRF-TOKEN",
18461
18453
  contentType: "Content-Type",
18462
18454
  accept: "Accept",
18455
+ apiKey: "X-Api-Key",
18456
+ projectKey: "Project-Key",
18457
+ };
18458
+ const API_BASE_URL = "/api/v1";
18459
+
18460
+ const create$2 = (ciBuildId, history_id, payload) => axios.post(`${API_BASE_URL}/reporter/runs/${ciBuildId}/test_entities/${history_id}/attempts`, {
18461
+ attempt: payload,
18462
+ });
18463
+ const update$1 = (ciBuildId, history_id, id, payload) => axios.put(`${API_BASE_URL}/reporter/runs/${ciBuildId}/test_entities/${history_id}/attempts/${id}`, {
18464
+ attempt: payload,
18465
+ });
18466
+ const attemptsApi = { create: create$2, update: update$1 };
18467
+
18468
+ const ERRORS = {
18469
+ onBegin: {
18470
+ failedToGetCommitSha: "Failed to get current commit SHA.",
18471
+ failedToGetCommitMessage: "Failed to get current commit message.",
18472
+ failedToInitializeRun: "Failed to initialize run in reporter",
18473
+ },
18474
+ onTestBegin: {
18475
+ failedToReportTest: (testTitle, historyId) => `Failed to report test "${testTitle}" with history ID ${historyId}`,
18476
+ },
18477
+ onEnd: {
18478
+ failedToReportRunStatus: "Failed to report run status",
18479
+ },
18463
18480
  };
18464
18481
 
18465
- const setAuthHeaders = (applicationKey) => {
18482
+ const MESSAGES = {
18483
+ onBegin: {
18484
+ testStarted: "Test has started reporting to neetoPlaywrightReporter",
18485
+ ciBuildId: (currentCiBuildId) => `CI BUILD ID: ${currentCiBuildId}`,
18486
+ totalShards: (totalShards) => `Total shards: ${totalShards}`,
18487
+ currentShard: (currentShard) => `Current shard: ${currentShard}`,
18488
+ },
18489
+ };
18490
+
18491
+ const consoleLogFormatted = {
18492
+ bold: (message) => console.log(console.log("\x1b[1m", message, "\x1b[0m")),
18493
+ dim: (message) => console.log(console.log("\x1b[2m", message, "\x1b[0m")),
18494
+ underline: (message) => console.log(console.log("\x1b[4m", message, "\x1b[0m")),
18495
+ invertBackground: (message) => console.log(console.log("\x1b[7m", message, "\x1b[0m")),
18496
+ hidden: (message) => console.log(console.log("\x1b[8m", message, "\x1b[0m")),
18497
+ error: (message) => console.log("\u001b[31m", "\x1b[1m", message, "\x1b[0m", "\u001b[0m"),
18498
+ warning: (message) => console.log("\u001b[33m", "\x1b[1m", message, "\x1b[0m", "\u001b[0m"),
18499
+ };
18500
+ const executeCommandLine = ({ command, messageOnError, shouldThrowError = false, logLevel = "warning", }) => {
18501
+ try {
18502
+ return childProcess__default["default"].execSync(command).toString().trim();
18503
+ }
18504
+ catch (err) {
18505
+ if (shouldThrowError) {
18506
+ throw err;
18507
+ }
18508
+ else {
18509
+ consoleLogFormatted[logLevel](messageOnError);
18510
+ }
18511
+ }
18512
+ };
18513
+
18514
+ const createShardObject = ({ currentShard = 0, status = "running", duration = null, }) => ({
18515
+ [currentShard]: { status, duration },
18516
+ });
18517
+
18518
+ const getDescribePath = ({ titlePath, title, project, spec, }) => titlePath.filter((item, index) => index !== 0 && item !== title && item !== project && item !== spec);
18519
+ const getCurrentCommitSha = () => executeCommandLine({
18520
+ command: "git rev-parse HEAD",
18521
+ messageOnError: ERRORS.onBegin.failedToGetCommitSha,
18522
+ });
18523
+ const getCurrentCommitMessage = () => executeCommandLine({
18524
+ command: "git show-branch --no-name HEAD",
18525
+ messageOnError: ERRORS.onBegin.failedToGetCommitMessage,
18526
+ });
18527
+ const getInitializerData = ({ rootDir }, rootSuite) => rootSuite.allTests().map(test => {
18528
+ var _a;
18529
+ const { title, parent, id: history_id, location: { file }, } = test;
18530
+ const titlePath = test.titlePath();
18531
+ const project = (_a = parent.project()) === null || _a === void 0 ? void 0 : _a.name;
18532
+ const spec = file.replace(`${rootDir}/`, "");
18533
+ const describe = getDescribePath({ titlePath, title, spec, project });
18534
+ return { title, describe, project, spec, history_id };
18535
+ });
18536
+
18537
+ const setAuthHeaders = (projectKey) => {
18538
+ var _a;
18466
18539
  axios.defaults.headers = {
18467
18540
  ...axios.defaults.headers,
18468
- [HEADERS_KEYS.applicationKey]: applicationKey,
18541
+ [HEADERS_KEYS.projectKey]: projectKey,
18542
+ [HEADERS_KEYS.apiKey]: (_a = process.env.API_KEY) !== null && _a !== void 0 ? _a : "",
18469
18543
  [HEADERS_KEYS.accept]: "application/json",
18470
18544
  [HEADERS_KEYS.contentType]: "application/json",
18471
18545
  };
18472
18546
  };
18473
- function initializeAxios(applicationKey) {
18474
- setAuthHeaders(applicationKey);
18547
+ function initializeAxios({ projectKey, baseURL, }) {
18548
+ axios.defaults.baseURL = baseURL;
18549
+ setAuthHeaders(projectKey);
18475
18550
  }
18476
18551
 
18552
+ const create$1 = (payload) => axios.post(`${API_BASE_URL}/reporter/runs`, {
18553
+ run: payload,
18554
+ });
18555
+ const update = (ciBuildId, payload) => axios.put(`${API_BASE_URL}/reporter/runs/${ciBuildId}`, {
18556
+ run: payload,
18557
+ });
18558
+ const runsApi = { create: create$1, update };
18559
+
18560
+ const create = (ciBuildId, payload) => axios.post(`${API_BASE_URL}/reporter/runs/${ciBuildId}/test_entities`, {
18561
+ test_entity: payload,
18562
+ });
18563
+ const testEntitiesApi = { create };
18564
+
18477
18565
  class MyReporter {
18478
18566
  constructor(options) {
18479
- this.onBegin = async (config, suite) => {
18480
- console.log("Run begin".padStart(5, "\n").padEnd(5, "\n"));
18481
- console.log("Config".padStart(20, "*").padEnd(40, "*"));
18482
- console.log(JSON.stringify(config, null, 4));
18483
- console.log("Suite".padStart(20, "*").padEnd(40, "*"));
18484
- const initializerData = suite.allTests().map(test => {
18485
- var _a;
18486
- const { rootDir } = config;
18487
- const { title, parent, id: history_id, location: { file }, } = test;
18488
- const titlePath = test.titlePath();
18489
- const project = (_a = parent.project()) === null || _a === void 0 ? void 0 : _a.name;
18490
- const spec = file.replace(`${rootDir}/`, "");
18491
- const describe = getDescribePath({ titlePath, title, spec, project });
18492
- return { title, describe, project, spec, history_id };
18493
- });
18494
- await dummyApi.create({ test_entity: { initializerData, config } });
18567
+ this.onBegin = async (config, rootSuite) => {
18568
+ const shard = config.shard;
18569
+ let attempts;
18570
+ try {
18571
+ const runDetails = {
18572
+ commit_id: getCurrentCommitSha(),
18573
+ commit_name: getCurrentCommitMessage(),
18574
+ ci_build_id: this.ciBuildId,
18575
+ configuration: config,
18576
+ shards: createShardObject({ currentShard: shard === null || shard === void 0 ? void 0 : shard.current }),
18577
+ };
18578
+ await runsApi.create(runDetails);
18579
+ ({ data: attempts } = await testEntitiesApi.create(this.ciBuildId, {
18580
+ test_entities: getInitializerData(config, rootSuite),
18581
+ }));
18582
+ }
18583
+ catch (_a) {
18584
+ throw new Error(ERRORS.onBegin.failedToInitializeRun);
18585
+ }
18586
+ consoleLogFormatted.underline(MESSAGES.onBegin.testStarted);
18587
+ consoleLogFormatted.dim(MESSAGES.onBegin.ciBuildId(this.ciBuildId));
18588
+ if (shard) {
18589
+ consoleLogFormatted.dim(MESSAGES.onBegin.totalShards(shard.total));
18590
+ consoleLogFormatted.dim(MESSAGES.onBegin.currentShard(shard.current));
18591
+ }
18592
+ this.attempts = attempts;
18593
+ this.config = config;
18594
+ this.currentShard = shard === null || shard === void 0 ? void 0 : shard.current;
18595
+ };
18596
+ this.onTestBegin = async ({ id, title }, { retry }) => {
18597
+ try {
18598
+ retry === 0 &&
18599
+ (await attemptsApi.update(this.ciBuildId, id, this.attempts[id], {
18600
+ status: "running",
18601
+ }));
18602
+ }
18603
+ catch (_a) {
18604
+ consoleLogFormatted.error(ERRORS.onTestBegin.failedToReportTest(title, id));
18605
+ }
18606
+ };
18607
+ this.onTestEnd = async ({ id, title }, { status, duration, errors, retry }) => {
18608
+ try {
18609
+ const testResult = {
18610
+ status,
18611
+ duration,
18612
+ log: errors.map(error => { var _a; return (_a = error.message) !== null && _a !== void 0 ? _a : ""; }).join("\n"),
18613
+ };
18614
+ retry === 0
18615
+ ? await attemptsApi.update(this.ciBuildId, id, this.attempts[id], testResult)
18616
+ : await attemptsApi.create(this.ciBuildId, id, testResult);
18617
+ }
18618
+ catch (_a) {
18619
+ consoleLogFormatted.error(ERRORS.onTestBegin.failedToReportTest(title, id));
18620
+ }
18621
+ };
18622
+ this.onEnd = async ({ status, duration }) => {
18623
+ try {
18624
+ await runsApi.update(this.ciBuildId, {
18625
+ shards: createShardObject({
18626
+ currentShard: this.currentShard,
18627
+ status,
18628
+ duration,
18629
+ }),
18630
+ });
18631
+ }
18632
+ catch (_a) {
18633
+ throw new Error(ERRORS.onEnd.failedToReportRunStatus);
18634
+ }
18495
18635
  };
18496
- initializeAxios(options.applicationKey);
18636
+ initializeAxios(options);
18637
+ this.attempts = {};
18638
+ this.ciBuildId = options.ciBuildId;
18497
18639
  }
18498
18640
  }
18499
18641