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

typescript: type this as IWorld in user functions #1690

Merged
merged 10 commits into from
Jun 4, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO

### Fixed

* `this` now explicitly typed in support code functions ([#1667](https://github.com/cucumber/cucumber-js/issues/1667) [#1690](https://github.com/cucumber/cucumber-js/pull/1690))
* Progress bar formatter now reports total step count correctly ([#1579](https://github.com/cucumber/cucumber-js/issues/1579)
[#1669](https://github.com/cucumber/cucumber-js/pull/1669))
* All messages now emitted with project-relative `uri`s
Expand Down
36 changes: 16 additions & 20 deletions src/support_code_library_builder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ export interface ITestStepHookParameter {
testStepId: string
}

export type TestCaseHookFunctionWithoutParameter = () => any | Promise<any>
export type TestCaseHookFunctionWithParameter = (
arg: ITestCaseHookParameter
export type TestCaseHookFunction = (
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
this: any,
arg?: ITestCaseHookParameter
) => any | Promise<any>
export type TestCaseHookFunction =
| TestCaseHookFunctionWithoutParameter
| TestCaseHookFunctionWithParameter

export type TestStepHookFunctionWithoutParameter = () => void
export type TestStepHookFunctionWithParameter = (
arg: ITestStepHookParameter
export type TestStepHookFunction = (
this: any,
arg?: ITestStepHookParameter
) => void
export type TestStepHookFunction =
| TestStepHookFunctionWithoutParameter
| TestStepHookFunctionWithParameter

export type TestStepFunction = (this: any, ...args: any[]) => any | Promise<any>

export interface IDefineStepOptions {
timeout?: number
Expand Down Expand Up @@ -67,11 +63,11 @@ export interface IParameterTypeDefinition<T> {

export interface IDefineSupportCodeMethods {
defineParameterType: (options: IParameterTypeDefinition<any>) => void
defineStep: ((pattern: DefineStepPattern, code: Function) => void) &
defineStep: ((pattern: DefineStepPattern, code: TestStepFunction) => void) &
((
pattern: DefineStepPattern,
options: IDefineStepOptions,
code: Function
code: TestStepFunction
) => void)
setDefaultTimeout: (milliseconds: number) => void
setDefinitionFunctionWrapper: (fn: Function) => void
Expand All @@ -92,23 +88,23 @@ export interface IDefineSupportCodeMethods {
((options: IDefineTestStepHookOptions, code: TestStepHookFunction) => void)
BeforeAll: ((code: Function) => void) &
((options: IDefineTestRunHookOptions, code: Function) => void)
Given: ((pattern: DefineStepPattern, code: Function) => void) &
Given: ((pattern: DefineStepPattern, code: TestStepFunction) => void) &
((
pattern: DefineStepPattern,
options: IDefineStepOptions,
code: Function
code: TestStepFunction
) => void)
Then: ((pattern: DefineStepPattern, code: Function) => void) &
Then: ((pattern: DefineStepPattern, code: TestStepFunction) => void) &
((
pattern: DefineStepPattern,
options: IDefineStepOptions,
code: Function
code: TestStepFunction
) => void)
When: ((pattern: DefineStepPattern, code: Function) => void) &
When: ((pattern: DefineStepPattern, code: TestStepFunction) => void) &
((
pattern: DefineStepPattern,
options: IDefineStepOptions,
code: Function
code: TestStepFunction
) => void)
}

Expand Down
33 changes: 33 additions & 0 deletions test-d/world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Before, setWorldConstructor, When, World } from '../'

// should allow us to read parameters and add attachments
Before(async function () {
await this.attach(this.parameters.foo)
})
When('stuff happens', async function () {
await this.attach(this.parameters.foo)
})

// should allow us to set and get arbitrary properties
Before(async function () {
this.bar = 'baz'
await this.log(this.baz)
})
When('stuff happens', async function () {
this.bar = 'baz'
await this.log(this.baz)
})

// should allow us to use a custom world class
class CustomWorld extends World {
doThing(): string {
return 'foo'
}
}
setWorldConstructor(CustomWorld)
Before(async function (this: CustomWorld) {
this.doThing()
})
When('stuff happens', async function (this: CustomWorld) {
this.doThing()
})