diff --git a/README.md b/README.md index e028791..9b9f2e6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 9c4e858..ab22169 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slack-ctrf", - "version": "0.0.10", + "version": "0.0.12", "description": "", "main": "index.js", "scripts": { diff --git a/src/cli.ts b/src/cli.ts index ba54435..90fb2b0 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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) => { @@ -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) { @@ -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) { @@ -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.'); diff --git a/src/message-formatter.ts b/src/message-formatter.ts index 9a5a712..93f39eb 100644 --- a/src/message-formatter.ts +++ b/src/message-formatter.ts @@ -1,6 +1,11 @@ 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; @@ -8,7 +13,7 @@ export const formatResultsMessage = (ctrf: CtrfReport): object => { 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"; @@ -105,15 +110,17 @@ 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); @@ -121,7 +128,7 @@ export const formatFlakyTestsMessage = (ctrf: CtrfReport): object | null => { 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";