Skip to content

Commit 60eff6c

Browse files
authored
Merge pull request #49 from test-results-reporter/29-when-milliseconds-are-0-the-colon-notation-function-fails-to-parse
fix: parsing 0 ms duration
2 parents 5d04c78 + dfae176 commit 60eff6c

9 files changed

+144
-66
lines changed

package-lock.json

+13-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
},
4040
"homepage": "https://test-results-reporter.github.io",
4141
"dependencies": {
42-
"colon-notation": "^1.2.1",
4342
"dotenv": "^14.3.0",
4443
"phin-retry": "^1.0.3",
44+
"pretty-ms": "^7.0.0",
4545
"rosters": "0.0.1",
4646
"sade": "^1.7.4",
4747
"test-results-parser": "^0.0.11"

src/helpers/helper.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const DATA_REF_PATTERN = /(\{[^\}]+\})/g;
2+
const pretty_ms = require('pretty-ms');
23

34
function getPercentage(x, y) {
45
if (y > 0) {
@@ -47,8 +48,13 @@ function truncate(text, length) {
4748
}
4849
}
4950

51+
function getPrettyDuration(ms, format) {
52+
return pretty_ms(parseInt(ms), { [format]: true, secondsDecimalDigits: 0 })
53+
}
54+
5055
module.exports = {
5156
getPercentage,
5257
processData,
5358
truncate,
59+
getPrettyDuration
5460
}

src/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface SlackInputs {
6363
only_failures?: boolean;
6464
title?: string;
6565
title_suffix?: string;
66+
duration?: string;
6667
}
6768

6869
export interface TeamsInputs {
@@ -72,6 +73,7 @@ export interface TeamsInputs {
7273
title?: string;
7374
title_suffix?: string;
7475
width?: string;
76+
duration?: string;
7577
}
7678

7779
export interface Target {

src/targets/slack.js

+6-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const request = require('phin-retry');
2-
const { getPercentage, truncate } = require('../helpers/helper');
3-
const { toColonNotation } = require('colon-notation');
2+
const { getPercentage, truncate, getPrettyDuration } = require('../helpers/helper');
43
const extension_manager = require('../extensions');
54
const { HOOK } = require('../helpers/constants');
65

@@ -44,7 +43,7 @@ function getMainPayload() {
4443
function setMainBlock({ result, target, payload }) {
4544
let text = `*${getTitleText(result, target)}*\n`;
4645
text += `\n*Results*: ${getResultText(result)}`;
47-
text += `\n*Duration*: ${getDurationText(result)}`;
46+
text += `\n*Duration*: ${getPrettyDuration(result.duration, target.inputs.duration)}`;
4847
payload.blocks.push({
4948
"type": "section",
5049
"text": {
@@ -67,10 +66,6 @@ function getResultText(result) {
6766
return `${result.passed} / ${result.total} Passed (${percentage}%)`;
6867
}
6968

70-
function getDurationText(result) {
71-
return `${toColonNotation(parseInt(result.duration))}`;
72-
}
73-
7469
function setSuiteBlock({ result, target, payload }) {
7570
if (target.inputs.include_suites) {
7671
for (let i = 0; i < result.suites.length; i++) {
@@ -80,7 +75,7 @@ function setSuiteBlock({ result, target, payload }) {
8075
}
8176
// if suites length eq to 1 then main block will include suite summary
8277
if (result.suites.length > 1) {
83-
payload.blocks.push(getSuiteSummary(suite));
78+
payload.blocks.push(getSuiteSummary({ target, suite }));
8479
}
8580
if (target.inputs.include_failure_details) {
8681
payload.blocks.push(getFailureDetails(suite));
@@ -89,23 +84,10 @@ function setSuiteBlock({ result, target, payload }) {
8984
}
9085
}
9186

92-
function getSuiteSummary(suite) {
93-
let text = `*${getSuiteTitle(suite)}*\n`;
94-
text += `\n*Results*: ${getResultText(suite)}`;
95-
text += `\n*Duration*: ${getDurationText(suite)}`;
96-
return {
97-
"type": "section",
98-
"text": {
99-
"type": "mrkdwn",
100-
"text": text
101-
}
102-
};
103-
}
104-
105-
function getSuiteSummary(suite) {
87+
function getSuiteSummary({ target, suite }) {
10688
let text = `*${getSuiteTitle(suite)}*\n`;
10789
text += `\n*Results*: ${getResultText(suite)}`;
108-
text += `\n*Duration*: ${getDurationText(suite)}`;
90+
text += `\n*Duration*: ${getPrettyDuration(suite.duration, target.inputs.duration )}`;
10991
return {
11092
"type": "section",
11193
"text": {
@@ -167,6 +149,7 @@ const default_inputs = {
167149
include_suites: true,
168150
only_failure_suites: false,
169151
include_failure_details: false,
152+
duration: 'colonNotation'
170153
}
171154

172155
module.exports = {

src/targets/teams.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const request = require('phin-retry');
2-
const { toColonNotation } = require('colon-notation');
3-
const { getPercentage, truncate } = require('../helpers/helper');
2+
const { getPercentage, truncate, getPrettyDuration } = require('../helpers/helper');
43
const extension_manager = require('../extensions');
54
const { HOOK } = require('../helpers/constants');
65

@@ -10,7 +9,7 @@ async function run({result, target}) {
109
const payload = getMainPayload(target);
1110
await extension_manager.run({ result, target, payload, root_payload, hook: HOOK.START });
1211
setTitleBlock(result, { target, payload });
13-
setMainBlock(result, { target, payload });
12+
setMainBlock({ result, target, payload });
1413
await extension_manager.run({ result, target, payload, root_payload, hook: HOOK.POST_MAIN });
1514
setSuiteBlock(result, { target, payload });
1615
await extension_manager.run({ result, target, payload, root_payload, hook: HOOK.END });
@@ -76,7 +75,7 @@ function setTitleBlock(result, { target, payload }) {
7675
});
7776
}
7877

79-
function setMainBlock(result, { payload }) {
78+
function setMainBlock({ result, target, payload }) {
8079
const facts = [];
8180
const percentage = getPercentage(result.passed, result.total);
8281
facts.push({
@@ -85,7 +84,7 @@ function setMainBlock(result, { payload }) {
8584
});
8685
facts.push({
8786
"title": "Duration:",
88-
"value": `${toColonNotation(parseInt(result.duration))}`
87+
"value": `${getPrettyDuration(result.duration, target.inputs.duration)}`
8988
});
9089
payload.body.push({
9190
"type": "FactSet",
@@ -102,7 +101,7 @@ function setSuiteBlock(result, { target, payload }) {
102101
}
103102
// if suites length eq to 1 then main block will include suite summary
104103
if (result.suites.length > 1) {
105-
payload.body.push(...getSuiteSummary(suite));
104+
payload.body.push(...getSuiteSummary({ suite, target }));
106105
}
107106
if (target.inputs.include_failure_details) {
108107
payload.body.push(...getFailureDetailsFactSets(suite));
@@ -111,7 +110,7 @@ function setSuiteBlock(result, { target, payload }) {
111110
}
112111
}
113112

114-
function getSuiteSummary(suite) {
113+
function getSuiteSummary({ suite, target }) {
115114
const percentage = getPercentage(suite.passed, suite.total);
116115
const emoji = suite.status === 'PASS' ? '✅' : '❌';
117116
return [
@@ -131,7 +130,7 @@ function getSuiteSummary(suite) {
131130
},
132131
{
133132
"title": "Duration:",
134-
"value": `${toColonNotation(parseInt(suite.duration))}`
133+
"value": `${getPrettyDuration(suite.duration, target.inputs.duration)}`
135134
}
136135
]
137136
}
@@ -187,7 +186,8 @@ const default_inputs = {
187186
include_suites: true,
188187
only_failure_suites: false,
189188
include_failure_details: false,
190-
width: ""
189+
width: '',
190+
duration: 'colonNotation'
191191
}
192192

193193
module.exports = {

test/mocks/slack.mock.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ addInteractionHandler('post test-summary to slack', () => {
1414
"type": "section",
1515
"text": {
1616
"type": "mrkdwn",
17-
"text": "*Default suite*\n\n*Results*: 4 / 4 Passed (100%)\n*Duration*: 00:02"
17+
"text": "*Default suite*\n\n*Results*: 4 / 4 Passed (100%)\n*Duration*: 0:02"
1818
}
1919
}
2020
]
@@ -49,14 +49,14 @@ addInteractionHandler('post test-summary to slack with multiple suites', () => {
4949
"type": "section",
5050
"text": {
5151
"type": "mrkdwn",
52-
"text": "*❌ desktop-chrome*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 03:22"
52+
"text": "*❌ desktop-chrome*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 3:22"
5353
}
5454
},
5555
{
5656
"type": "section",
5757
"text": {
5858
"type": "mrkdwn",
59-
"text": "*❌ mobile-ios*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 09:05"
59+
"text": "*❌ mobile-ios*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 9:05"
6060
}
6161
}
6262
]
@@ -119,7 +119,7 @@ addInteractionHandler('post failure-details to slack with multiple suites', () =
119119
"type": "section",
120120
"text": {
121121
"type": "mrkdwn",
122-
"text": "*❌ desktop-chrome*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 03:22"
122+
"text": "*❌ desktop-chrome*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 3:22"
123123
}
124124
},
125125
{
@@ -133,7 +133,7 @@ addInteractionHandler('post failure-details to slack with multiple suites', () =
133133
"type": "section",
134134
"text": {
135135
"type": "mrkdwn",
136-
"text": "*❌ mobile-ios*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 09:05"
136+
"text": "*❌ mobile-ios*\n\n*Results*: 2 / 5 Passed (40%)\n*Duration*: 9:05"
137137
}
138138
},
139139
{
@@ -168,7 +168,7 @@ addInteractionHandler('post failure-details to slack with single suite', () => {
168168
"type": "section",
169169
"text": {
170170
"type": "mrkdwn",
171-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
171+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
172172
}
173173
},
174174
{
@@ -203,7 +203,7 @@ addInteractionHandler('post test-summary with hyperlinks to slack - pass status'
203203
"type": "section",
204204
"text": {
205205
"type": "mrkdwn",
206-
"text": "*Default suite*\n\n*Results*: 4 / 4 Passed (100%)\n*Duration*: 00:02"
206+
"text": "*Default suite*\n\n*Results*: 4 / 4 Passed (100%)\n*Duration*: 0:02"
207207
}
208208
},
209209
{
@@ -240,7 +240,7 @@ addInteractionHandler('post test-summary with hyperlinks to slack - fail status'
240240
"type": "section",
241241
"text": {
242242
"type": "mrkdwn",
243-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
243+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
244244
}
245245
},
246246
{
@@ -277,7 +277,7 @@ addInteractionHandler('post test-summary to slack with report portal analysis',
277277
"type": "section",
278278
"text": {
279279
"type": "mrkdwn",
280-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
280+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
281281
}
282282
},
283283
{
@@ -312,7 +312,7 @@ addInteractionHandler('post test-summary to slack with report portal analysis wi
312312
"type": "section",
313313
"text": {
314314
"type": "mrkdwn",
315-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
315+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
316316
}
317317
},
318318
{
@@ -350,7 +350,7 @@ addInteractionHandler('post test-summary with mentions to slack', () => {
350350
"type": "section",
351351
"text": {
352352
"type": "mrkdwn",
353-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
353+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
354354
}
355355
},
356356
{
@@ -385,7 +385,7 @@ addInteractionHandler('post test-summary to slack with qc-test-summary', (ctx) =
385385
"type": "section",
386386
"text": {
387387
"type": "mrkdwn",
388-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
388+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
389389
},
390390
"accessory": {
391391
"type": "image",
@@ -418,7 +418,7 @@ addInteractionHandler('post test-summary to slack with report portal history', (
418418
"type": "section",
419419
"text": {
420420
"type": "mrkdwn",
421-
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
421+
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 0:02"
422422
}
423423
},
424424
{

0 commit comments

Comments
 (0)