Skip to content

Commit bb088ff

Browse files
authored
Support named BeforeAll/AfterAll hooks (#2661)
1 parent 63cc0ce commit bb088ff

File tree

8 files changed

+37
-5
lines changed

8 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber.
99

1010
## [Unreleased]
11+
### Added
12+
- Support named BeforeAll/AfterAll hooks ([#2661](https://github.com/cucumber/cucumber-js/pull/2661))
1113

1214
## [12.2.0] - 2025-08-22
1315
### Added

docs/support_files/api_reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Multiple `After` hooks are executed in the **reverse** order that they are defin
5454
Defines a hook which is run after all scenarios have completed.
5555

5656
* `options`: An object with the following keys:
57+
* `name`: An optional name for this hook
5758
* `timeout`: A hook-specific timeout, to override the default timeout.
5859
* `fn`: A function, defined as follows:
5960
* When using the asynchronous callback interface, have one argument for the callback function.

src/cli/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function emitTestRunHooks(
195195
hook: {
196196
id: hook.id,
197197
type,
198+
name: hook.name,
198199
sourceReference: makeSourceReference(hook),
199200
},
200201
} satisfies Envelope)

src/cli/helpers_spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ describe('helpers', () => {
287287
unwrappedCode: noopFunction,
288288
id: '1',
289289
line: 7,
290-
options: {},
290+
options: {
291+
name: 'special cleanup thing',
292+
},
291293
uri: 'features/support/run-hooks.js',
292294
}),
293295
new TestRunHookDefinition({
@@ -306,6 +308,7 @@ describe('helpers', () => {
306308
hook: {
307309
id: '0',
308310
type: HookType.BEFORE_TEST_RUN,
311+
name: undefined,
309312
sourceReference: {
310313
uri: 'features/support/run-hooks.js',
311314
location: {
@@ -318,6 +321,7 @@ describe('helpers', () => {
318321
hook: {
319322
id: '1',
320323
type: HookType.AFTER_TEST_RUN,
324+
name: 'special cleanup thing',
321325
sourceReference: {
322326
uri: 'features/support/run-hooks.js',
323327
location: {
@@ -330,6 +334,7 @@ describe('helpers', () => {
330334
hook: {
331335
id: '2',
332336
type: HookType.AFTER_TEST_RUN,
337+
name: undefined,
333338
sourceReference: {
334339
uri: 'features/support/run-hooks.js',
335340
location: {
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
import Definition from './definition'
1+
import Definition, {
2+
IDefinitionParameters,
3+
IDefinitionOptions,
4+
} from './definition'
25

3-
export default class TestRunHookDefinition extends Definition {}
6+
export interface ITestRunHookDefinitionOptions extends IDefinitionOptions {
7+
name?: string
8+
}
9+
10+
export default class TestRunHookDefinition extends Definition {
11+
public readonly name: string
12+
13+
constructor(data: IDefinitionParameters<ITestRunHookDefinitionOptions>) {
14+
super(data)
15+
this.name = data.options.name
16+
}
17+
}

src/support_code_library_builder/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
} from '@cucumber/cucumber-expressions'
88
import TestCaseHookDefinition from '../models/test_case_hook_definition'
99
import TestStepHookDefinition from '../models/test_step_hook_definition'
10-
import TestRunHookDefinition from '../models/test_run_hook_definition'
10+
import TestRunHookDefinition, {
11+
ITestRunHookDefinitionOptions,
12+
} from '../models/test_run_hook_definition'
1113
import StepDefinition from '../models/step_definition'
1214
import { formatLocation } from '../formatter/helpers'
1315
import { doesHaveValue } from '../value_checker'
@@ -355,7 +357,7 @@ export class SupportCodeLibraryBuilder {
355357
code: wrappedCode,
356358
id: canonicalIds ? canonicalIds[index] : this.newId(),
357359
line,
358-
options,
360+
options: options as ITestRunHookDefinitionOptions,
359361
unwrappedCode: code,
360362
uri,
361363
})

src/support_code_library_builder/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface IDefineTestStepHookOptions {
6767
}
6868

6969
export interface IDefineTestRunHookOptions {
70+
name?: string
7071
timeout?: number
7172
}
7273

test-d/hooks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ Before(async function () {
5050
After(async function () {
5151
return 'skipped'
5252
})
53+
54+
// should allow named hooks
55+
BeforeAll({ name: 'before test run' }, function () {})
56+
AfterAll({ name: 'after test run' }, function () {})
57+
Before({ name: 'before test case' }, function () {})
58+
After({ name: 'after test case' }, function () {})

0 commit comments

Comments
 (0)