Skip to content

Commit

Permalink
feat: allow custom title
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma11hewThomas committed Jul 30, 2024
1 parent 4edec57 commit 68a2f59
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,24 @@ or using the alias:
npx slack-ctrf results /path/to/ctrf-file.json -f
```

### Custom Notification Title

To can choose a custom title for your notification, use the `--title` option:

```sh
npx slack-ctrf results /path/to/ctrf-file.json --title "Custom Title"
```

or using the alias:

```sh
npx slack-ctrf results /path/to/ctrf-file.json -t "Custom Title"
```

## Options

- `--onFailOnly, -f`: Send notification only if there are failed tests.
- `--title, -t`: Title of the notification.

## Merge reports

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slack-ctrf",
"version": "0.0.10",
"version": "0.0.12",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 21 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const argv = yargs(hideBin(process.argv))
type: 'boolean',
description: 'Send message only if there are failed tests',
default: false,
})
.option('title', {
alias: 't',
type: 'string',
description: 'Title of notification',
default: "Test Results",
});
},
async (argv) => {
Expand All @@ -30,7 +36,7 @@ const argv = yargs(hideBin(process.argv))
console.log('No failed tests. Message not sent.');
return;
}
const message = formatResultsMessage(ctrfData);
const message = formatResultsMessage(ctrfData, {title: argv.title});
await sendSlackMessage(message);
console.log('Results message sent to Slack.');
} catch (error: any) {
Expand All @@ -46,12 +52,18 @@ const argv = yargs(hideBin(process.argv))
describe: 'Path to the CTRF file',
type: 'string',
demandOption: true,
})
.option('title', {
alias: 't',
type: 'string',
description: 'Title of notification',
default: "Failed Tests",
});
},
async (argv) => {
try {
const ctrfData = parseCtrfFile(argv.path as string);
const message = formatFailedTestsMessage(ctrfData);
const message = formatFailedTestsMessage(ctrfData, {title: argv.title});
// await sendSlackMessage(message);
console.log('Coming soon!');
} catch (error: any) {
Expand All @@ -67,12 +79,18 @@ const argv = yargs(hideBin(process.argv))
describe: 'Path to the CTRF file',
type: 'string',
demandOption: true,
})
.option('title', {
alias: 't',
type: 'string',
description: 'Title of notification',
default: "Flaky Tests",
});
},
async (argv) => {
try {
const ctrfData = parseCtrfFile(argv.path as string);
const message = formatFlakyTestsMessage(ctrfData);
const message = formatFlakyTestsMessage(ctrfData, {title: argv.title});
if (message) {
await sendSlackMessage(message);
console.log('Flaky tests message sent to Slack.');
Expand Down
17 changes: 12 additions & 5 deletions src/message-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { CtrfReport } from '../types/ctrf';

export const formatResultsMessage = (ctrf: CtrfReport): object => {
type Options =
{
title: string
}

export const formatResultsMessage = (ctrf: CtrfReport, options?: Options): object => {
const { summary, environment } = ctrf.results;
const passedTests = summary.passed;
const failedTests = summary.failed;
const skippedTests = summary.skipped;
const pendingTests = summary.pending;
const otherTests = summary.other;

let title = "CTRF Test Results";
let title = options?.title ? options?.title : "Test Results";
let missingEnvProperties: string[] = [];

let buildInfo = "*Build:* No build information provided";
Expand Down Expand Up @@ -105,23 +110,25 @@ export const formatResultsMessage = (ctrf: CtrfReport): object => {
};


export const formatFailedTestsMessage = (ctrf: CtrfReport): string => {
export const formatFailedTestsMessage = (ctrf: CtrfReport, options?: Options): string => {
const failedTests = ctrf.results.tests.filter(test => test.status === 'failed');
if (failedTests.length === 0) return 'No failed tests.';

let title = options?.title ? options?.title : "Failed Tests";

const message = failedTests.map(test => `Test: ${test.name}\nMessage: ${test.message}\n`).join('\n');
return `Failed Tests:\n${message}`;
};

export const formatFlakyTestsMessage = (ctrf: CtrfReport): object | null => {
export const formatFlakyTestsMessage = (ctrf: CtrfReport, options?: Options): object | null => {
const { summary, environment, tests } = ctrf.results;
const flakyTests = tests.filter(test => test.flaky);

if (flakyTests.length === 0) {
return null;
}

let title = "CTRF Flaky Test Report";
let title = options?.title ? options?.title : "Flaky Tests";
let missingEnvProperties: string[] = [];

let buildInfo = "Build: No build information provided";
Expand Down

0 comments on commit 68a2f59

Please sign in to comment.