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

[Docs] This argument as first parameter to step definition function #1948

Closed
Izhaki opened this issue Mar 2, 2022 · 3 comments
Closed

[Docs] This argument as first parameter to step definition function #1948

Izhaki opened this issue Mar 2, 2022 · 3 comments
Labels
📖 documentation Improvements or additions to documentation

Comments

@Izhaki
Copy link
Contributor

Izhaki commented Mar 2, 2022

In #1667 (comment) the proposed solution by @davidjgoss is:

import {Then, World} from '@cucumber/cucumber';

Then('the test runs with string parameter {string}', async function(this: World, parameter: string) {
    this.driver.close()
})

I could not find any mention of this in the docs. Further, 20m of code surfing, and I couldn't even find where this is actually happening.

If this an official API? This changes things quite a bit as it unlocks the use of arrow functions.

We would like our world to be typed, and right now this is the only way to do this. So we need insurance this API is official.

@Izhaki Izhaki added 🐛 bug Defect / Bug 📖 documentation Improvements or additions to documentation and removed 🐛 bug Defect / Bug labels Mar 2, 2022
@davidjgoss
Copy link
Contributor

Hey @Izhaki, bit of a misunderstanding here. The context of that issue and my comment was TypeScript, which allows you to type this for a function by prepending it to the arguments - but it's not an actual argument and is compiled out. See https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function. A quick playground to demo:

image

Unfortunately the limitation of arrow functions is unchanged. You might consider trying https://github.com/jbpros/cucumber-fp for an approach that skirts the this issue.

@davidjgoss
Copy link
Contributor

davidjgoss commented Mar 2, 2022

We would like our world to be typed, and right now this is the only way to do this. So we need insurance this API is official.

To answer this specifically: yes if you're using TypeScript then whatever class you pass to setWorldConstructor you can use as the this arg on step/hook functions and you should get the type safety you need.

@Izhaki
Copy link
Contributor Author

Izhaki commented Mar 2, 2022

Awesome. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📖 documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants