-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
[Request] Better support for worlds in TypeScript #864
Comments
Yeah, no. The type is a correct reflection of the reality and useful in other regards. You can extend the type with your own properties, as shown below. // declarations.d.ts
interface CustomWorld {
count: number;
eat: (count: number) => void;
}
declare namespace Mocha {
interface Context extends CustomWorld {}
}
// steps.ts
import { Given } from "@badeball/cypress-cucumber-preprocessor";
Given("foo", function () {
this.eat(123);
}); |
I know that you can extend In |
We're already provided a context by simply existing in the Cypress domain. How do your propose that we retain this context and expose it to users that need it, and allow for generic contexts? |
I'm not sure if I understand your question. export declare function defineStep<T extends unknown[], C extends Mocha.Context>(description: string | RegExp, implementation: IStepDefinitionBody<T, C>): void;
export interface IStepDefinitionBody<T extends unknown[], C extends Mocha.Context> {
(this: C, ...args: T): void;
} so that each feature file can potentially define its own "world": import { Given } from "@badeball/cypress-cucumber-preprocessor";
interface CustomWorld extends Mocha.Context {
homepage: HomePageDriver;
}
Given("I'm on the homepage", function (this: CustomWorld) {
cy.visit("/");
this.homepage = new HomePageDriver(cy);
}); |
Aha, I see. If it doesn't affect current types and allows more extensibility, then I'd be open for it. Feel free to give it a shot and open up a PR. Edit: I imagine maybe a generic that extends with a default à |
Released as v13.1.0. |
Current behavior
When using TypeScript it's not possible to override the type associated to
this
.For example the following step definition:
will raise:
Desired behavior
A workaround for the time being is to define
this
asunknown
and then use a type assertion:The desired behaviour would be to have the function context not hardcoded to
Mocha.Context
, similar tocucumber-js
, so that the first example could compile without any errors.Test code to reproduce
Versions
Checklist
[email protected]
(package name has changed and it is no longer the most recent version, see #689).The text was updated successfully, but these errors were encountered: