diff --git a/lib/test.decorator.ts b/lib/test.decorator.ts index b15ccc4..7da0784 100644 --- a/lib/test.decorator.ts +++ b/lib/test.decorator.ts @@ -10,7 +10,7 @@ interface TestDecoratorOptions { class TestDecorator implements TestDecoratorOptions { name: string; - constructor(private testMethod: any, private testMethodContext: any, options: TestDecoratorOptions) { + constructor(private testMethod: any, options: TestDecoratorOptions) { this.name = testMethod.name; Object.assign(this, options); @@ -19,7 +19,7 @@ class TestDecorator implements TestDecoratorOptions { private wrapTest(testCode: () => Promise, wrapperCode: (args: (() => Promise)) => Promise) { return new Proxy(testCode, { apply: (target: any, thisArg: any, argArray: any[]) => - wrapperCode(() => target.apply(this.testMethodContext, argArray)) + wrapperCode(() => target.apply(thisArg, argArray)) }) } @@ -27,8 +27,8 @@ class TestDecorator implements TestDecoratorOptions { 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); } @@ -40,12 +40,12 @@ export type TestDecoratedMethod = { testDecorator: TestDecorator }; * 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); }); } diff --git a/tests/test.spec.ts b/tests/test.spec.ts index 33df192..8aeab2f 100644 --- a/tests/test.spec.ts +++ b/tests/test.spec.ts @@ -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', () => {