Repository: test-results-reporter/reporter
Pull request #160 (2024-03-25 17:01:18)

feat: fix bug


Patch:
diff --git a/package-lock.json b/package-lock.json
index 5daafe5..8a587fe 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "test-results-reporter",
-  "version": "1.1.4",
+  "version": "1.1.5",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "test-results-reporter",
-      "version": "1.1.4",
+      "version": "1.1.5",
       "license": "ISC",
       "dependencies": {
         "async-retry": "^1.3.3",
diff --git a/package.json b/package.json
index b6f18b3..c7aeb1b 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "test-results-reporter",
-  "version": "1.1.4",
+  "version": "1.1.5",
   "description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
   "main": "src/index.js",
   "types": "./src/index.d.ts",
diff --git a/src/beats/index.js b/src/beats/index.js
index 5a5f99b..8697822 100644
--- a/src/beats/index.js
+++ b/src/beats/index.js
@@ -9,7 +9,7 @@ const BASE_URL = process.env.TEST_BEATS_URL || "http://localhost:9393";
  * @param {TestResult} result
  */
 async function run(config, result) {
-  if (config.project && config.build && config.api_key) {
+  if (config.project && config.run && config.api_key) {
     const run_id = await publishTestResults(config, result);
     if (run_id) {
       attachTestBeatsReportHyperLink(config, run_id);
@@ -25,7 +25,7 @@ async function publishTestResults(config, result) {
   try {
     const payload = {
       project: config.project,
-      build: config.build,
+      run: config.run,
       ...result
     }
     const ci = getCIInformation();
diff --git a/src/index.d.ts b/src/index.d.ts
index 6ac70c3..d3dbfde 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -221,7 +221,7 @@ export interface CustomResultOptions {
 export interface PublishReport {
   api_key?: string;
   project?: string;
-  build?: string;
+  run?: string;
   targets?: Target[];
   results?: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[];
 }
@@ -229,7 +229,7 @@ export interface PublishReport {
 export interface PublishConfig {
   api_key?: string;
   project?: string;
-  build?: string;
+  run?: string;
   targets?: Target[];
   results?: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[];
   reports?: PublishReport[];
diff --git a/src/targets/chat.js b/src/targets/chat.js
index 86a6441..ede57d7 100644
--- a/src/targets/chat.js
+++ b/src/targets/chat.js
@@ -66,9 +66,10 @@ function setMainBlock({ result, target, payload }) {
 }
 
 function setSuiteBlock({ result, target, payload }) {
+  let suite_attachments_length = 0;
   if (target.inputs.include_suites) {
     let texts = [];
-    for (let i = 0; i < result.suites.length && i < target.inputs.max_suites; i++) {
+    for (let i = 0; i < result.suites.length && suite_attachments_length < target.inputs.max_suites; i++) {
       const suite = result.suites[i];
       if (target.inputs.only_failures && suite.status !== 'FAIL') {
         continue;
@@ -76,7 +77,7 @@ function setSuiteBlock({ result, target, payload }) {
       // if suites length eq to 1 then main block will include suite summary
       if (result.suites.length > 1) {
         texts.push(getSuiteSummary({ target, suite }));
-
+        suite_attachments_length += 1;
       }
       if (target.inputs.include_failure_details) {
         texts.push(getFailureDetails(suite));
diff --git a/src/targets/slack.js b/src/targets/slack.js
index dfead54..c59c85f 100644
--- a/src/targets/slack.js
+++ b/src/targets/slack.js
@@ -83,8 +83,9 @@ function getResultText(result) {
 }
 
 function setSuiteBlock({ result, target, payload }) {
+  let suite_attachments_length = 0;
   if (target.inputs.include_suites) {
-    for (let i = 0; i < result.suites.length && i < target.inputs.max_suites; i++) {
+    for (let i = 0; i < result.suites.length && suite_attachments_length < target.inputs.max_suites; i++) {
       const suite = result.suites[i];
       if (target.inputs.only_failures && suite.status !== 'FAIL') {
         continue;
@@ -92,6 +93,7 @@ function setSuiteBlock({ result, target, payload }) {
       // if suites length eq to 1 then main block will include suite summary
       if (result.suites.length > 1) {
         payload.blocks.push(getSuiteSummary({ target, suite }));
+        suite_attachments_length += 1;
       }
       if (target.inputs.include_failure_details) {
         // Only attach failure details block if there were failures
diff --git a/src/targets/teams.js b/src/targets/teams.js
index 6cc5b06..7f1ab41 100644
--- a/src/targets/teams.js
+++ b/src/targets/teams.js
@@ -122,8 +122,9 @@ function setMainBlock({ result, target, payload }) {
 }
 
 function setSuiteBlock({ result, target, payload }) {
+  let suite_attachments_length = 0;
   if (target.inputs.include_suites) {
-    for (let i = 0; i < result.suites.length && i < target.inputs.max_suites; i++) {
+    for (let i = 0; i < result.suites.length && suite_attachments_length < target.inputs.max_suites; i++) {
       const suite = result.suites[i];
       if (target.inputs.only_failures && suite.status !== 'FAIL') {
         continue;
@@ -131,6 +132,7 @@ function setSuiteBlock({ result, target, payload }) {
       // if suites length eq to 1 then main block will include suite summary
       if (result.suites.length > 1) {
         payload.body.push(...getSuiteSummary({ suite, target }));
+        suite_attachments_length += 1;
       }
       if (target.inputs.include_failure_details) {
         payload.body.push(...getFailureDetailsFactSets(suite));


Test patch:
diff --git a/test/beats.spec.js b/test/beats.spec.js
index 81b4f99..459e62a 100644
--- a/test/beats.spec.js
+++ b/test/beats.spec.js
@@ -11,7 +11,7 @@ describe('TestBeats', () => {
       config: {
         api_key: 'api-key',
         project: 'project-name',
-        build: 'build-name',
+        run: 'build-name',
         targets: [
           {
             name: 'teams',
@@ -41,7 +41,7 @@ describe('TestBeats', () => {
       config: {
         api_key: 'api-key',
         project: 'project-name',
-        build: 'build-name',
+        run: 'build-name',
         targets: [
           {
             name: 'teams',
diff --git a/test/mocks/slack.mock.js b/test/mocks/slack.mock.js
index b23eb1c..fcb49c7 100644
--- a/test/mocks/slack.mock.js
+++ b/test/mocks/slack.mock.js
@@ -680,4 +680,31 @@ addInteractionHandler('post test-summary with multiple suites and ci-info to to
       status: 200
     }
   }
+});
+
+addInteractionHandler('post test-summary to slack with max suites as 1', () => {
+  return {
+    request: {
+      method: 'POST',
+      path: '/message',
+      body: {
+        "attachments": [
+          {
+            "color": "#DC143C",
+            "blocks": [
+              {
+                "@DATA:TEMPLATE@": "SLACK_ROOT_MULTIPLE_SUITES"
+              },
+              {
+                "@DATA:TEMPLATE@": "SLACK_SUITE_CHROME"
+              }
+            ]
+          }
+        ]
+      }
+    },
+    response: {
+      status: 200
+    }
+  }
 });
\ No newline at end of file
diff --git a/test/targets.slack.spec.js b/test/targets.slack.spec.js
index b3bc35c..caabb41 100644
--- a/test/targets.slack.spec.js
+++ b/test/targets.slack.spec.js
@@ -182,6 +182,36 @@ describe('targets - slack - functional', () => {
     assert.equal(mock.getInteraction(id).exercised, true);
   });
 
+  it('should send test-summary using max_suites as 1', async () => {
+    const id = mock.addInteraction('post test-summary to slack with max suites as 1');
+    await publish({
+      config: {
+        "reports": [
+          {
+            "targets": [
+              {
+                "name": "slack",
+                "inputs": {
+                  "url": "http://localhost:9393/message",
+                  "max_suites": 1
+                }
+              }
+            ],
+            "results": [
+              {
+                "type": "testng",
+                "files": [
+                  "test/data/testng/multiple-suites.xml"
+                ]
+              }
+            ]
+          }
+        ]
+      }
+    });
+    assert.equal(mock.getInteraction(id).exercised, true);
+  });
+
   afterEach(() => {
     mock.clearInteractions();
   });