getgauge/gauge-ts

Cannot refer to "this" in step implemention

yo7 opened this issue · 3 comments

yo7 commented

Describe the bug
This might be question or feature request rather than bug report, but when I declare class member xxx in step implementation class and runs test, I get the error Cannot read property 'xxx' of undefined.

To Reproduce

Run test with spec file like this:

# Sign up

## Sign Up with email

* Click sign up button

And step implementation like this:

export default class SignupSteps {
  signUpButton: ButtonWrapper = button("Sign Up")

  @Step("Click sign up button")
  public async clickSignUpButton() {
    await click(this.signUpButton)
  }
}

Expected behavior

I thought class property can be accessed via this and can be used to share variable between steps, and it would be the intuitive behavior.

Is it expected behavior that this is undefined and cannot be used in steps?

Desktop (please complete the following information):

  • OS: macOS
  • Gauge and plugin version [run gauge -v]: 1.1.5
  • Node version [run node -v]: 12.19.0
  • Npm version [run npm -v]: 6.14.8

I'm experiencing the same behavior, but in order to reproduce I additionally import an ES6 module into the .ts file that implements the step. In that case, I notice that the file won't be loaded here: https://github.com/BugDiver/gauge-ts/blob/8987ae99e76c17ce0959f865f21a8d9304973101/src/loaders/ImplLoader.ts#L19
I simply added some log statements (console.log(c);console.log(c.default)) after that line to see what happens. The first log statement doesn't output anything, the second outputs undefined. and therefore the registry isn't updated with an instance for 'this'.

Now, to get the whole ES6 import deal working at all, I'm relying on the package ESM.

Hope this provides some additional context.

@yo7 You should be able to access properties. But in your case, I don't think it's the right thing to do, because when the class will be initialized the browser won't be started to (initialization happens before executing steps/hooks), so not sure what the call button() will return.

@yo7, @lordstyx Please provide a sample project which can be used to replicate the issue

As I'm quite new to js development it took me a while to sort out the dependencies for my test project. However, now that that's done (no need for the ESM package anymore), the this variable is no longer undefined.
In my opinion, and I can't speak for @yo7 of course, there's no need for you to fix anything, as it's not really broken.