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

fix: fix this context in @test method #9

Merged
merged 1 commit into from
Jul 11, 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
18 changes: 9 additions & 9 deletions lib/test.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
class TestDecorator implements TestDecoratorOptions {
name: string;

constructor(private testMethod: any, private testMethodContext: any, options: TestDecoratorOptions) {
constructor(private testMethod: any, options: TestDecoratorOptions) {

Check warning on line 13 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
this.name = testMethod.name;

Object.assign(this, options);
}

private wrapTest(testCode: () => Promise<any>, wrapperCode: (args: (() => Promise<any>)) => Promise<any>) {

Check warning on line 19 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 19 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 19 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return new Proxy(testCode, {
apply: (target: any, thisArg: any, argArray: any[]) =>

Check warning on line 21 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 21 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 21 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
wrapperCode(() => target.apply(this.testMethodContext, argArray))
wrapperCode(() => target.apply(thisArg, argArray))
})
}

private runTest(userTestCode: () => Promise<any>) {
return userTestCode();
}

run() {
const testCallback = this.wrapTest(this.testMethod, this.runTest).bind(this.testMethodContext);
run(executionContext: any) {
const testCallback = this.wrapTest(this.testMethod, this.runTest).bind(executionContext);

playwright(this.name, testCallback);
}
Expand All @@ -40,12 +40,12 @@
* Mark method as test.
* Method class should be marked with @suite decorator
*/
export const test = (options: TestDecoratorOptions = {}) => function(originalMethod: any, context: ClassMemberDecoratorContext) {
const testDecorator = new TestDecorator(originalMethod, context, options);
export const test = (options: TestDecoratorOptions = {}) => function(originalMethod: any, context: any) {
const testDecorator = new TestDecorator(originalMethod, options);

Object.assign(originalMethod, { testDecorator });

context.addInitializer(function () {
testDecorator.run();
(context as ClassMemberDecoratorContext ).addInitializer(function () {
testDecorator.run(this);
});
}
12 changes: 9 additions & 3 deletions tests/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ playwright.describe('@test decorator', () => {
testMethod() {
called.push('testMethod');
}

@test()
testMethod2() {
called.push('testMethod2');
}

notTestMethod() {
called.push('notTestMethod');
}

@test()
testThisContext() {
called.push('testThisContext');
expect(this instanceof ExampleSuite).toBeTruthy();
}
}

playwright('Methods with @test should be run', () => {
expect(called).toEqual(expect.arrayContaining(['testMethod', 'testMethod2']));
expect(called).toEqual(expect.arrayContaining(['testMethod', 'testMethod2', 'testThisContext']));
});

playwright('Methods without @test should not be run', () => {
Expand Down
Loading