You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run jest on the following minimal test with a basic decorator.
functiondecorator(target: Object,propertyKey: string,descriptor: PropertyDescriptor,){constoriginalMethod=descriptor.value;descriptor.value=function(...args: any[]){// BUG: `this` is undefined when run with, e.g., expect(decoratedFcn).toThrow()console.log(`Log instance var; should not be undefined: ${JSON.stringify(this)}`,);returnoriginalMethod.apply(this,args);};}classSimpleClass{constructor(publicname: string){}
@decoratorlogme(){// BUG: when called from `expect(simpleClassInstance.logme).not.toThrow()`, `this` is undefconsole.log(this.name);}}test("jest decorator",()=>{constsc=newSimpleClass("somename");sc.logme();// this works// this fails: `this` is not available in the decorated logme()expect(sc.logme).not.toThrow();});
Expected behavior
expect(obj.decoratedFcn).not.toThrow()
should behave the same as if we just called obj.decoratedFcn()
Actual behavior
expect(decoratedFunction).toThrow() results in this (which should be the instance on which the decoratedFunction is being called) being undefined
The issue is observed in both
in descriptor.value redefinition (this is undefined)
in the original decorated function: this in logme() is undefined
Version
29.7.0
Steps to reproduce
Run jest on the following minimal test with a basic decorator.
Expected behavior
expect(obj.decoratedFcn).not.toThrow()
should behave the same as if we just called
obj.decoratedFcn()
Actual behavior
expect(decoratedFunction).toThrow()
results inthis
(which should be the instance on which the decoratedFunction is being called) being undefinedThe issue is observed in both
this
is undefined)this
in logme() is undefinedAdditional context
No response
Environment
The text was updated successfully, but these errors were encountered: