Skip to content

Commit

Permalink
feat: added dependency manager for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Nov 24, 2024
1 parent 110aec2 commit 146cc5d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
23 changes: 23 additions & 0 deletions reporters/dependency-reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
import { TestDependencyManager } from '@/test-utils/dependency-manager';

export default class DependencyReporter implements Reporter {
private manager = TestDependencyManager.getInstance();
private skippedTests: Map<string, string> = new Map();

onTestEnd(test: TestCase, result: TestResult) {
if (result.status === 'skipped') {
this.skippedTests.set(
test.title,
this.manager.getDependencyFailureReason(test.title),
);
}
}

onEnd() {
console.log('\nDependency Failure Report:');
this.skippedTests.forEach((reason, test) => {
console.log(`${test}: Skipped due to: ${reason}`);
});
}
}
21 changes: 21 additions & 0 deletions test-utils/base-fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test as base } from '@playwright/test';
import { TestDependencyManager } from '@/test-utils/dependency-manager';

type BaseFixtures = {
dependencyManager: TestDependencyManager;
testInfo: { id: string };
};

export const test = base.extend<BaseFixtures>({
dependencyManager: async ({}, use) => {
const manager = TestDependencyManager.getInstance();
await use(manager);
},

testInfo: [async ({}, use, testInfo) => {
const info = {
id: testInfo.title,
};
await use(info);
}, { auto: true }],
});
41 changes: 41 additions & 0 deletions test-utils/dependency-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export class TestDependencyManager {
private static instance: TestDependencyManager;
private statusMap: Map<string, boolean> = new Map();
private dependencyGraph: Map<string, string[]> = new Map();
private errorMessages: Map<string, string> = new Map();

private constructor() {
}

static getInstance(): TestDependencyManager {
if (!TestDependencyManager.instance) {
TestDependencyManager.instance = new TestDependencyManager();
}
return TestDependencyManager.instance;
}

registerTest(testId: string, dependencies: string[] = []) {
this.dependencyGraph.set(testId, dependencies);
this.statusMap.set(testId, false);
}

setTestStatus(testId: string, passed: boolean, errorMessage?: string) {
this.statusMap.set(testId, passed);
if (!passed && errorMessage) {
this.errorMessages.set(testId, errorMessage);
}
}

canRunTest(testId: string): boolean {
const dependencies = this.dependencyGraph.get(testId) || [];
return dependencies.every(depId => this.statusMap.get(depId));
}

getDependencyFailureReason(testId: string): string {
const dependencies = this.dependencyGraph.get(testId) || [];
const failedDeps = dependencies.filter(depId => !this.statusMap.get(depId));
return failedDeps
.map(depId => `${depId}: ${this.errorMessages.get(depId)}`)
.join(', ');
}
}
3 changes: 2 additions & 1 deletion tests/login/login.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { test as base, expect } from '@playwright/test';
import { expect } from '@playwright/test';
import { LoginPage } from '@/page-objects/views/login/loginPage';
import { test as base } from '@/test-utils/base-fixtures';
import userData from '@/fixtures/userData.json';
import { TAG_PRIORITY_CRITICAL } from '@/constants/priority';

Expand Down
Empty file removed utils/.gitkeep
Empty file.

0 comments on commit 146cc5d

Please sign in to comment.