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: onlyCi mode #7

Merged
merged 8 commits into from
Mar 30, 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
6 changes: 5 additions & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
node-version: 16.x
cache: 'pnpm'
- run: pnpm install
- run: pnpm run build
- run: pnpm build

test:
runs-on: ubuntu-latest
Expand All @@ -37,4 +37,8 @@ jobs:
node-version: 16.x
cache: 'pnpm'
- run: pnpm install
- run: pnpm build
- run: pnpm test
env:
TWITCH_USERNAME: ${{secrets.TWITCH_USERNAME}}
TWITCH_PASSWORD: ${{secrets.TWITCH_PASSWORD}}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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
messagesOnWatchMode: false // report or not test results to twitch in watch mode (default false)
messagesOnWatchMode: false, // report or not test results to twitch in watch mode (default false)
onlyCI: false // report or not test results to twitch only on CI environments (default false)
},
],
],
Expand Down
19 changes: 10 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ module.exports = {
// Use this configuration option to add custom reporters to Jest
reporters: [
'default',
// [
// './dist/src/index.js',
// {
// channels: ['SantiMA10'],
// username: process.env.TWITCH_USERNAME,
// password: process.env.TWITCH_PASSWORD,
// messagesOnWatchMode: false,
// },
// ],
[
'./dist/src/index.js',
{
channels: ['SantiMA10'],
username: process.env.TWITCH_USERNAME,
password: process.env.TWITCH_PASSWORD,
messagesOnWatchMode: false,
onlyCI: true,
},
],
],

// Automatically reset mock state before every test
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@santima10/jest-chat-reporter",
"version": "0.2.0",
"version": "0.3.0",
"publishConfig": {
"access": "public"
},
Expand All @@ -16,7 +16,7 @@
"scripts": {
"lint": "eslint . --ext .ts --ignore-path .gitignore",
"build": "tsc",
"test": "jest",
"test": "jest --forceExit",
"test:watch": "jest --watch",
"prepublish": "yarn build"
},
Expand All @@ -39,6 +39,7 @@
"@santima10/eslint-config": "^2.2.0",
"@swc/core": "^1.2.160",
"@swc/jest": "^0.2.20",
"@types/env-ci": "^3.1.1",
"@types/jest": "^27.4.1",
"@types/tmi.js": "^1.8.1",
"@typescript-eslint/eslint-plugin": "^5.16.0",
Expand All @@ -57,6 +58,7 @@
},
"prettier": "@santima10/eslint-config/.prettierrc.json",
"dependencies": {
"env-ci": "^7.1.0",
"tmi.js": "^1.8.5"
}
}
42 changes: 26 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AggregatedResult, Config, Context, Reporter } from '@jest/reporters';
import tmi from 'tmi.js';

import { TwitchChatService } from './services/chat/TwitchChatService';
import { EnvCIEnvironmentService } from './services/EnvCIEnvironmentService';
import { ReportTestResults } from './useCases/ReportTestResults';

export default class TwitchJestReporter implements Pick<Reporter, 'onRunComplete'> {
Expand All @@ -14,6 +15,7 @@ export default class TwitchJestReporter implements Pick<Reporter, 'onRunComplete
username: string;
password: string;
messagesOnWatchMode?: boolean;
onlyCI?: boolean;
},
) {
this.tmiClient = new tmi.Client({
Expand All @@ -26,9 +28,14 @@ export default class TwitchJestReporter implements Pick<Reporter, 'onRunComplete
}

public onRunComplete = async (_: Set<Context>, results: AggregatedResult): Promise<void> => {
await new ReportTestResults(new TwitchChatService(this.tmiClient), {
messagesOnWatchMode: !!this.reporterConfig.messagesOnWatchMode,
}).run({
await new ReportTestResults(
new TwitchChatService(this.tmiClient),
new EnvCIEnvironmentService(),
{
messagesOnWatchMode: !!this.reporterConfig.messagesOnWatchMode,
onlyCI: !!this.reporterConfig.onlyCI,
},
).run({
...results,
runningOnWatchMode: this.globalConfig.watch || this.globalConfig.watchAll,
});
Expand Down
32 changes: 32 additions & 0 deletions src/services/EnvCIEnvironmentService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import envCi from 'env-ci';

import { EnvironmentService } from './EnvironmentService';

export class EnvCIEnvironmentService implements EnvironmentService {
public constructor(private readonly env: typeof envCi = envCi) {}

public isCI: EnvironmentService['isCI'] = () => this.env().isCi;

public getServiceName: EnvironmentService['getServiceName'] = () => {
const ci = this.env();
return ci.isCi ? ci.service : 'local';
};

public getBuildUrl: EnvironmentService['getBuildUrl'] = () => {
const ci = this.env();

if (!ci.isCi) {
return 'localhost';
}

if ('buildUrl' in ci && !!ci.buildUrl) {
return ci.buildUrl;
}

if ('slug' in ci) {
return ci.slug;
}

return 'localhost';
};
}
5 changes: 5 additions & 0 deletions src/services/EnvironmentService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface EnvironmentService {
isCI: () => boolean;
getServiceName: () => string;
getBuildUrl: () => string;
}
Loading