@stanterprise/playwright-reporter 0.0.2 → 0.0.7

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.
@@ -2,4 +2,4 @@ import { FullConfig } from "@playwright/test";
2
2
  import { Suite } from "@playwright/test/reporter";
3
3
  import { StanterpriseReporterOptions } from "../types";
4
4
  import * as grpc from "@grpc/grpc-js";
5
- export declare function handleOnBeginEvent(config: FullConfig, suite: Suite, runId: string, client: grpc.Client, options: StanterpriseReporterOptions): void;
5
+ export declare function handleOnBeginEvent(config: FullConfig, suite: Suite, name: string, runId: string, client: grpc.Client, options: StanterpriseReporterOptions): void;
@@ -5,11 +5,13 @@ const events_1 = require("@stanterprise/protobuf/testsystem/v1/events");
5
5
  const grpcClient_1 = require("../client/grpcClient");
6
6
  const entities_1 = require("@stanterprise/protobuf/testsystem/v1/entities");
7
7
  const utils_1 = require("../utils");
8
- function handleOnBeginEvent(config, suite, runId, client, options) {
8
+ function handleOnBeginEvent(config, suite, name, runId, client, options) {
9
9
  // Report root suite and all child suites recursively
10
10
  const request = new events_1.MapTestRunEventRequest({
11
11
  run_id: runId,
12
+ name: name,
12
13
  test_suites: mapSuites(suite, runId),
14
+ total_tests: suite.allTests().length,
13
15
  });
14
16
  (0, grpcClient_1.reportUnary)(options, client, "/testsystem.v1.observer.TestEventCollector/MapTestRun", request, options.grpcTimeout).catch((e) => {
15
17
  const details = e instanceof Error ? `${e.message}` : String(e);
@@ -1 +1,4 @@
1
- export declare function handleOnEndEvent(): void;
1
+ import { FullResult } from "@playwright/test/reporter";
2
+ import { StanterpriseReporterOptions } from "../types";
3
+ import * as grpc from "@grpc/grpc-js";
4
+ export declare function handleOnEndEvent(result: FullResult, runId: string, client: grpc.Client, options: StanterpriseReporterOptions): void;
@@ -1,4 +1,51 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handleOnEndEvent = handleOnEndEvent;
4
- function handleOnEndEvent() { }
4
+ const grpcClient_1 = require("../client/grpcClient");
5
+ const utils_1 = require("../utils");
6
+ const events_1 = require("@stanterprise/protobuf/testsystem/v1/events");
7
+ const common_1 = require("@stanterprise/protobuf/testsystem/v1/common");
8
+ function mapTestStatus(status) {
9
+ switch (status) {
10
+ case "passed":
11
+ return common_1.TestStatus.PASSED;
12
+ case "failed":
13
+ return common_1.TestStatus.FAILED;
14
+ case "timedout":
15
+ return common_1.TestStatus.TIMEDOUT;
16
+ case "interrupted":
17
+ return common_1.TestStatus.INTERRUPTED;
18
+ default:
19
+ return common_1.TestStatus.UNKNOWN;
20
+ }
21
+ }
22
+ function handleOnEndEvent(result, runId, client, options) {
23
+ // Validate and sanitize inputs to prevent serialization failures
24
+ const startTimeMs = result.startTime?.getTime();
25
+ const duration = result.duration;
26
+ // Ensure valid numeric values (protobuf cannot serialize NaN or undefined)
27
+ const validStartTimeMs = typeof startTimeMs === "number" && !isNaN(startTimeMs)
28
+ ? startTimeMs
29
+ : Date.now();
30
+ const validDuration = typeof duration === "number" && !isNaN(duration) && duration >= 0
31
+ ? duration
32
+ : 0;
33
+ const request = new events_1.TestRunEndEventRequest({
34
+ run_id: runId,
35
+ final_status: mapTestStatus(result.status),
36
+ start_time: (0, utils_1.createTimestampFromMs)(validStartTimeMs),
37
+ duration: (0, utils_1.createDuration)(validDuration),
38
+ });
39
+ (0, grpcClient_1.reportUnary)(options, client, "/testsystem.v1.observer.TestEventCollector/ReportEnd", request, options.grpcTimeout).catch((error) => {
40
+ if (error instanceof Error) {
41
+ console.error(`Failed to report end event: ${error.message}`, {
42
+ stack: error.stack,
43
+ });
44
+ }
45
+ else {
46
+ console.error("Failed to report end event: Unknown error type", {
47
+ error,
48
+ });
49
+ }
50
+ });
51
+ }
package/dist/reporter.js CHANGED
@@ -47,7 +47,7 @@ class StanterpriseReporter {
47
47
  console.log(`Run started at: ${this.runStartTime.toISOString()}`);
48
48
  }
49
49
  if (this.options.grpcEnabled) {
50
- (0, handlers_1.handleOnBeginEvent)(config, suite, this.runId, this.grpcClient, this.options);
50
+ (0, handlers_1.handleOnBeginEvent)(config, suite, process.env.STANTERPRISE_TEST_RUN_NAME || "Playwright Test Run", this.runId, this.grpcClient, this.options);
51
51
  }
52
52
  }
53
53
  async onExit() {
@@ -69,6 +69,9 @@ class StanterpriseReporter {
69
69
  console.log(`Run start time: ${this.runStartTime.toISOString()}`);
70
70
  console.log(`Playwright start time: ${result.startTime.toISOString()}`);
71
71
  }
72
+ if (this.options.grpcEnabled) {
73
+ (0, handlers_1.handleOnEndEvent)(result, this.runId, this.grpcClient, this.options);
74
+ }
72
75
  return Promise.resolve();
73
76
  }
74
77
  onTestBegin(test, result) {
@@ -12,15 +12,24 @@ const Timestamp = protobuf_1.google.google.protobuf.Timestamp;
12
12
  * Create a protobuf Timestamp from a JavaScript Date
13
13
  */
14
14
  function createTimestamp(date) {
15
+ const ms = date.getTime();
16
+ // Validate to prevent NaN from causing protobuf serialization failures
17
+ if (isNaN(ms)) {
18
+ throw new Error(`Invalid date provided to createTimestamp: ${date.toString()}`);
19
+ }
15
20
  return new Timestamp({
16
- seconds: Math.floor(date.getTime() / 1000),
17
- nanos: (date.getTime() % 1000) * 1000000,
21
+ seconds: Math.floor(ms / 1000),
22
+ nanos: (ms % 1000) * 1000000,
18
23
  });
19
24
  }
20
25
  /**
21
26
  * Create a protobuf Timestamp from milliseconds since epoch
22
27
  */
23
28
  function createTimestampFromMs(ms) {
29
+ // Validate to prevent NaN from causing protobuf serialization failures
30
+ if (typeof ms !== "number" || isNaN(ms)) {
31
+ throw new Error(`Invalid milliseconds provided to createTimestampFromMs: ${ms}`);
32
+ }
24
33
  return new Timestamp({
25
34
  seconds: Math.floor(ms / 1000),
26
35
  nanos: (ms % 1000) * 1000000,
@@ -38,6 +47,13 @@ function createTimestampFromMs(ms) {
38
47
  * @returns A Duration object (implemented as Timestamp due to export limitations)
39
48
  */
40
49
  function createDuration(durationMs) {
50
+ // Validate to prevent NaN from causing protobuf serialization failures
51
+ if (typeof durationMs !== "number" || isNaN(durationMs)) {
52
+ throw new Error(`Invalid duration provided to createDuration: ${durationMs}`);
53
+ }
54
+ if (durationMs < 0) {
55
+ throw new Error(`Negative duration provided to createDuration: ${durationMs}`);
56
+ }
41
57
  return new Timestamp({
42
58
  seconds: Math.floor(durationMs / 1000),
43
59
  nanos: (durationMs % 1000) * 1000000,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stanterprise/playwright-reporter",
3
- "version": "0.0.2",
3
+ "version": "0.0.7",
4
4
  "description": "Custom Playwright test reporter that sends test results to Stanterprise via gRPC",
5
5
  "homepage": "https://github.com/stanterprise/stanterprise-playwright-reporter#readme",
6
6
  "bugs": {
@@ -16,8 +16,7 @@
16
16
  "main": "dist/index.js",
17
17
  "types": "dist/index.d.ts",
18
18
  "files": [
19
- "dist/**/*",
20
- "README.md"
19
+ "dist/**/*"
21
20
  ],
22
21
  "keywords": [
23
22
  "playwright",
@@ -54,6 +53,6 @@
54
53
  "@grpc/grpc-js": "^1.14.1",
55
54
  "@protobuf-ts/grpc-transport": "^2.11.1",
56
55
  "@protobuf-ts/grpcweb-transport": "^2.11.1",
57
- "@stanterprise/protobuf": "^0.0.15"
56
+ "@stanterprise/protobuf": "^0.0.18"
58
57
  }
59
58
  }