Skip to content

Commit

Permalink
Support separate Slack channels for successful and failed test results (
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kusnierz authored Jan 11, 2024
1 parent 3e836bf commit 7719f5d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ An example advanced configuration is shown below:

### **channels**
An array of Slack channels to post to, at least one channel is required
### **onSuccessChannels**
(Optional) An array of Slack channels to post to when tests have passed. Value from `channels` is used if not defined here
### **onFailureChannels**
(Optional) An array of Slack channels to post to when tests have failed. Value from `channels` is used if not defined here
### **sendResults**
Can either be *"always"*, *"on-failure"* or *"off"*, this configuration is required:
* **always** - will send the results to Slack at completion of the test run
Expand Down
37 changes: 28 additions & 9 deletions src/SlackReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class SlackReporter implements Reporter {

private sendResults: 'always' | 'on-failure' | 'off' = 'on-failure';

private slackChannels: string[] = [];
private onSuccessSlackChannels: string[] = [];

private onFailureSlackChannels: string[] = [];

private slackLogLevel: LogLevel | undefined;

Expand Down Expand Up @@ -68,7 +70,10 @@ class SlackReporter implements Reporter {
this.sendResults = slackReporterConfig.sendResults || 'always';
this.customLayout = slackReporterConfig.layout;
this.customLayoutAsync = slackReporterConfig.layoutAsync;
this.slackChannels = slackReporterConfig.channels;
this.onSuccessSlackChannels
= slackReporterConfig.onSuccessChannels || slackReporterConfig.channels;
this.onFailureSlackChannels
= slackReporterConfig.onFailureChannels || slackReporterConfig.channels;
this.maxNumberOfFailuresToShow
= slackReporterConfig.maxNumberOfFailuresToShow || 10;
this.slackOAuthToken = slackReporterConfig.slackOAuthToken || undefined;
Expand Down Expand Up @@ -98,12 +103,14 @@ class SlackReporter implements Reporter {
);
resultSummary.meta = this.meta;
const maxRetry = Math.max(...resultSummary.tests.map((o) => o.retry));
const testsFailed = resultSummary.tests.some(
(z) => (z.status === 'failed' || z.status === 'timedOut')
&& z.retry === maxRetry,
);

if (
this.sendResults === 'on-failure'
&& resultSummary.tests.filter(
(z) => (z.status === 'failed' || z.status === 'timedOut')
&& z.retry === maxRetry,
).length === 0
&& !testsFailed
) {
this.log('⏩ Slack reporter - no failures found');
return;
Expand Down Expand Up @@ -134,9 +141,11 @@ class SlackReporter implements Reporter {
),
);

const slackChannels = testsFailed ? this.onFailureSlackChannels : this.onSuccessSlackChannels;

const result = await slackClient.sendMessage({
options: {
channelIds: this.slackChannels,
channelIds: slackChannels,
customLayout: this.customLayout,
customLayoutAsync: this.customLayoutAsync,
maxNumberOfFailures: this.maxNumberOfFailuresToShow,
Expand Down Expand Up @@ -208,10 +217,20 @@ class SlackReporter implements Reporter {
};
}

if (!this.sendResults || this.slackChannels?.length === 0) {
const noSuccessChannelsProvided = this.sendResults === 'always' && (!this.onSuccessSlackChannels || this.onSuccessSlackChannels.length === 0);
const noFailureChannelsProvided = ['always', 'on-failure'].includes(this.sendResults) && (!this.onFailureSlackChannels || this.onFailureSlackChannels.length === 0);

if (noSuccessChannelsProvided) {
return {
okToProceed: false,
message: '❌ Slack channel(s) for successful tests notifications was not provided in the config',
};
}

if (noFailureChannelsProvided) {
return {
okToProceed: false,
message: '❌ Slack channel(s) was not provided in the config',
message: '❌ Slack channel(s) for failed tests notifications was not provided in the config',
};
}

Expand Down
12 changes: 9 additions & 3 deletions tests/SlackReporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ test.describe('SlackReporter - preChecks()', () => {
const result = fakeSlackReporter.preChecks();
expect(result).toEqual({
okToProceed: false,
message: '❌ Slack channel(s) was not provided in the config',
message: '❌ Slack channel(s) for successful tests notifications was not provided in the config',
});
});

Expand Down Expand Up @@ -292,7 +292,10 @@ test.describe('SlackReporter - preChecks()', () => {
cloneFullConfig.reporter = [
[
'/home/ry/_repo/playwright-slack-report/src/SlackReporter.ts',
{ layout: 'not a function' },
{
layout: 'not a function',
channels: ['zeb', 'pw'],
},
],
];
fakeSlackReporter.onBegin(cloneFullConfig, suite);
Expand All @@ -313,7 +316,10 @@ test.describe('SlackReporter - preChecks()', () => {
cloneFullConfig.reporter = [
[
'/home/ry/_repo/playwright-slack-report/src/SlackReporter.ts',
{ meta: 'not a array' },
{
meta: 'not a array',
channels: ['zeb', 'pw'],
},
],
];
fakeSlackReporter.onBegin(cloneFullConfig, suite);
Expand Down

0 comments on commit 7719f5d

Please sign in to comment.