Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new configuration to skip report on watch mode #5

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
{
channels: [], // an array with the twitch channels names the report is going to send messages
username: '', // an string with the username for the twitch chat
password: '' // an string with the oauth2 token for the twitch chat
password: '', // an string with the oauth2 token for the twitch chat
messagesOnWatchMode: false // report or not test results to twitch in watch mode (default false)
},
],
],
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module.exports = {
// channels: ['SantiMA10'],
// username: process.env.TWITCH_USERNAME,
// password: process.env.TWITCH_PASSWORD,
// messagesOnWatchMode: false,
// },
// ],
],
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": "@santima10/jest-chat-reporter",
"version": "0.1.1",
"version": "0.2.0",
"publishConfig": {
"access": "public"
},
Expand Down
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AggregatedResult, Context, Reporter } from '@jest/reporters';
import { AggregatedResult, Config, Context, Reporter } from '@jest/reporters';
import tmi from 'tmi.js';

import { TwitchChatService } from './services/chat/TwitchChatService';
Expand All @@ -8,8 +8,13 @@ export default class TwitchJestReporter implements Pick<Reporter, 'onRunComplete
private tmiClient: tmi.Client;

public constructor(
_: Record<string, unknown>,
reporterConfig: { channels: string[]; username: string; password: string },
private globalConfig: Config.GlobalConfig,
private reporterConfig: {
channels: string[];
username: string;
password: string;
messagesOnWatchMode?: boolean;
},
) {
this.tmiClient = new tmi.Client({
identity: {
Expand All @@ -21,6 +26,11 @@ export default class TwitchJestReporter implements Pick<Reporter, 'onRunComplete
}

public onRunComplete = async (_: Set<Context>, results: AggregatedResult): Promise<void> => {
await new ReportTestResults(new TwitchChatService(this.tmiClient)).run(results);
await new ReportTestResults(new TwitchChatService(this.tmiClient), {
messagesOnWatchMode: !!this.reporterConfig.messagesOnWatchMode,
}).run({
...results,
runningOnWatchMode: this.globalConfig.watch || this.globalConfig.watchAll,
});
};
}
10 changes: 9 additions & 1 deletion src/useCases/ReportTestResults.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { ChatService } from '../services/chat/ChatService';

export class ReportTestResults {
public constructor(private chatService: ChatService) {}
public constructor(
private chatService: ChatService,
private config: { messagesOnWatchMode: boolean } = { messagesOnWatchMode: false },
) {}

public async run(results: {
numFailedTests: number;
numPassedTests: number;
numTotalTests: number;
runningOnWatchMode?: boolean;
}): Promise<void> {
if (!this.config.messagesOnWatchMode && results.runningOnWatchMode) {
return;
}

if (results.numPassedTests > 0) {
await this.chatService.say(
`✅ ${results.numPassedTests}/${results.numTotalTests} tests passed`,
Expand Down
28 changes: 28 additions & 0 deletions tests/useCases/ReportTestResults.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,33 @@ describe('ReportTestResults', () => {
expect(chatService.say).toHaveBeenNthCalledWith(1, '❌ 1/1 tests failed');
expect(chatService.say).not.toHaveBeenNthCalledWith(2, '✅ 0/1 tests passed');
});

it('sent messages if the tests are running in watch mode and the watch mode is enabled', async () => {
const chatService = new ChatServiceMock();
const subject = new ReportTestResults(chatService, { messagesOnWatchMode: true });

await subject.run({
numFailedTests: 1,
numPassedTests: 0,
numTotalTests: 1,
runningOnWatchMode: true,
});

expect(chatService.say).toHaveBeenCalled();
});

it('does not sent messages if the tests are running in watch mode and the watch mode is disabled', async () => {
const chatService = new ChatServiceMock();
const subject = new ReportTestResults(chatService);

await subject.run({
numFailedTests: 1,
numPassedTests: 0,
numTotalTests: 1,
runningOnWatchMode: true,
});

expect(chatService.say).not.toHaveBeenCalled();
});
});
});