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

refactor: refactor suite and test decorators #5

Merged
merged 1 commit into from
Jul 10, 2023
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
38 changes: 32 additions & 6 deletions lib/suite.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import playwright from '@playwright/test';

// eslint-disable-next-line @typescript-eslint/ban-types
type Clazz = { new (...args: any[]): {} }
class SuiteDecorator {
onBeforeTestListeners: (() => void)[] = [];

/**
* Run after test.describe() and before test() calls
* @param callback
*/
onBeforeTest(callback: () => void) {
this.onBeforeTestListeners.push(callback);
}

private runOnBeforeTestListeners() {
this.onBeforeTestListeners.forEach(hook => hook());
}

run(constructor: any) {
playwright.describe(constructor.name, () => {
this.runOnBeforeTestListeners();
new constructor();
});
}
}

export type SuiteDecoratedMethod = { suiteDecorator: SuiteDecorator };

/**
* Mark class as test suite
*/
export function suite<T extends Clazz>(constructor: T) {
playwright.describe(constructor.name, () => {
new constructor();
});
export function suite<T extends { new (...args: any[]): any }>(constructor: T, context?: ClassMethodDecoratorContext) {
const suiteDecorator = new SuiteDecorator();

Object.assign(constructor, { suiteDecorator });

context?.addInitializer(() => {
suiteDecorator.run(constructor);
})
}
39 changes: 36 additions & 3 deletions lib/test.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import playwright from '@playwright/test';

class TestDecorator {
onBeforeTestListeners: (() => void)[] = [];

/**
* Run inside test() call, before original test code execution
* @param callback
*/
onBeforeTest(callback: () => void) {
this.onBeforeTestListeners.push(callback);
}

private runOnBeforeTestListeners() {
this.onBeforeTestListeners.forEach(hook => hook());
}

run(constructor: any, context: any) {
const constructorProxy = new Proxy(constructor, {
apply: (target: any, thisArg: any, argArray: any[]) => {
this.runOnBeforeTestListeners();
return target.apply(context, argArray);
}
})

playwright(constructor.name, constructorProxy.bind(context));
}
}

export type TestDecoratedMethod = { testDecorator: TestDecorator };

/**
* Mark class method as test
* Mark @suite class method as test
*/
export function test(originalMethod: any, context: ClassMethodDecoratorContext) {
export function test(originalMethod: any, context: ClassMemberDecoratorContext) {
const testDecorator = new TestDecorator();

Object.assign(originalMethod, { testDecorator });

context.addInitializer(function () {
playwright(originalMethod.name, originalMethod.bind(this));
testDecorator.run(originalMethod, this);
});
}