Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add global hooks ([#10](https://github.com/cucumber/javascript-core/pull/10))

## [0.3.0] - 2025-07-30
### Added
Expand Down
70 changes: 45 additions & 25 deletions cucumber-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,6 @@ export class DataTable {
transpose(): DataTable;
}

// @public
export type DefinedHook = {
id: string;
name?: string;
tags?: {
raw: string;
compiled: ReturnType<typeof parse>;
};
fn: SupportCodeFunction;
sourceReference: SourceReference;
toMessage(): Hook;
};

// @public
export type DefinedParameterType = {
id: string;
Expand All @@ -102,6 +89,28 @@ export type DefinedStep = {
toMessage(): StepDefinition;
};

// @public
export type DefinedTestCaseHook = {
id: string;
name?: string;
tags?: {
raw: string;
compiled: ReturnType<typeof parse>;
};
fn: SupportCodeFunction;
sourceReference: SourceReference;
toMessage(): Hook;
};

// @public
export type DefinedTestRunHook = {
id: string;
name?: string;
fn: SupportCodeFunction;
sourceReference: SourceReference;
toMessage(): Hook;
};

// @public
export function makeTestPlan(ingredients: TestPlanIngredients, options?: TestPlanOptions): AssembledTestPlan;

Expand All @@ -111,14 +120,6 @@ export type MatchedStep = {
args: ReadonlyArray<Argument>;
};

// @public
export interface NewHook {
fn: SupportCodeFunction;
name?: string;
sourceReference: SourceReference;
tags?: string;
}

// @public
export interface NewParameterType {
name: string;
Expand All @@ -136,6 +137,21 @@ export interface NewStep {
sourceReference: SourceReference;
}

// @public
export interface NewTestCaseHook {
fn: SupportCodeFunction;
name?: string;
sourceReference: SourceReference;
tags?: string;
}

// @public
export interface NewTestRunHook {
fn: SupportCodeFunction;
name?: string;
sourceReference: SourceReference;
}

// @public
export type PreparedFunction = {
fn: SupportCodeFunction;
Expand All @@ -144,8 +160,10 @@ export type PreparedFunction = {

// @public
export interface SupportCodeBuilder {
afterHook(options: NewHook): SupportCodeBuilder;
beforeHook(options: NewHook): SupportCodeBuilder;
afterAllHook(options: NewTestRunHook): SupportCodeBuilder;
afterHook(options: NewTestCaseHook): SupportCodeBuilder;
beforeAllHook(options: NewTestRunHook): SupportCodeBuilder;
beforeHook(options: NewTestCaseHook): SupportCodeBuilder;
build(): SupportCodeLibrary;
parameterType(options: NewParameterType): SupportCodeBuilder;
step(options: NewStep): SupportCodeBuilder;
Expand All @@ -156,9 +174,11 @@ export type SupportCodeFunction = (...args: any[]) => any | Promise<any>;

// @public
export interface SupportCodeLibrary {
findAllAfterHooksBy(tags: ReadonlyArray<string>): ReadonlyArray<DefinedHook>;
findAllBeforeHooksBy(tags: ReadonlyArray<string>): ReadonlyArray<DefinedHook>;
findAllAfterHooksBy(tags: ReadonlyArray<string>): ReadonlyArray<DefinedTestCaseHook>;
findAllBeforeHooksBy(tags: ReadonlyArray<string>): ReadonlyArray<DefinedTestCaseHook>;
findAllStepsBy(text: string): ReadonlyArray<MatchedStep>;
getAllAfterAllHooks(): ReadonlyArray<DefinedTestRunHook>;
getAllBeforeAllHooks(): ReadonlyArray<DefinedTestRunHook>;
toEnvelopes(): ReadonlyArray<Envelope>;
}

Expand Down
78 changes: 69 additions & 9 deletions src/SupportCodeBuilderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import parse from '@cucumber/tag-expressions'

import { SupportCodeLibraryImpl } from './SupportCodeLibraryImpl'
import {
DefinedHook,
DefinedParameterType,
DefinedStep,
NewHook,
DefinedTestCaseHook,
DefinedTestRunHook,
NewParameterType,
NewStep,
NewTestCaseHook,
NewTestRunHook,
SupportCodeBuilder,
UndefinedParameterType,
} from './types'
Expand All @@ -29,8 +31,10 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {
private readonly undefinedParameterTypes: Map<string, Set<string>> = new Map()
private readonly parameterTypes: Array<WithId<NewParameterType>> = []
private readonly steps: Array<WithId<NewStep>> = []
private readonly beforeHooks: Array<WithId<NewHook>> = []
private readonly afterHooks: Array<WithId<NewHook>> = []
private readonly beforeHooks: Array<WithId<NewTestCaseHook>> = []
private readonly afterHooks: Array<WithId<NewTestCaseHook>> = []
private readonly beforeAllHooks: Array<WithId<NewTestRunHook>> = []
private readonly afterAllHooks: Array<WithId<NewTestRunHook>> = []

constructor(private readonly newId: () => string) {}

Expand All @@ -42,15 +46,15 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {
return this
}

beforeHook(options: NewHook) {
beforeHook(options: NewTestCaseHook) {
this.beforeHooks.push({
id: this.newId(),
...options,
})
return this
}

afterHook(options: NewHook) {
afterHook(options: NewTestCaseHook) {
this.afterHooks.push({
id: this.newId(),
...options,
Expand All @@ -66,6 +70,22 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {
return this
}

beforeAllHook(options: NewTestRunHook) {
this.beforeAllHooks.push({
id: this.newId(),
...options,
})
return this
}

afterAllHook(options: NewTestRunHook) {
this.afterAllHooks.push({
id: this.newId(),
...options,
})
return this
}

private buildParameterTypes(): ReadonlyArray<DefinedParameterType> {
return this.parameterTypes.map((registered) => {
const parameterType = new ParameterType(
Expand Down Expand Up @@ -155,7 +175,7 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {
.flat()
}

private buildBeforeHooks(): ReadonlyArray<DefinedHook> {
private buildBeforeHooks(): ReadonlyArray<DefinedTestCaseHook> {
return this.beforeHooks.map(({ id, name, tags, fn, sourceReference }) => {
return {
id,
Expand All @@ -181,7 +201,7 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {
})
}

private buildAfterHooks(): ReadonlyArray<DefinedHook> {
private buildAfterHooks(): ReadonlyArray<DefinedTestCaseHook> {
return this.afterHooks.map(({ id, name, tags, fn, sourceReference }) => {
return {
id,
Expand All @@ -207,13 +227,53 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {
})
}

private buildBeforeAllHooks(): ReadonlyArray<DefinedTestRunHook> {
return this.beforeAllHooks.map(({ id, name, fn, sourceReference }) => {
return {
id,
name,
fn,
sourceReference,
toMessage() {
return {
id,
type: HookType.BEFORE_TEST_RUN,
name,
sourceReference,
}
},
}
})
}

private buildAfterAllHooks(): ReadonlyArray<DefinedTestRunHook> {
return this.afterAllHooks.map(({ id, name, fn, sourceReference }) => {
return {
id,
name,
fn,
sourceReference,
toMessage() {
return {
id,
type: HookType.AFTER_TEST_RUN,
name,
sourceReference,
}
},
}
})
}

build() {
return new SupportCodeLibraryImpl(
this.buildParameterTypes(),
this.buildSteps(),
this.buildUndefinedParameterTypes(),
this.buildBeforeHooks(),
this.buildAfterHooks()
this.buildAfterHooks(),
this.buildBeforeAllHooks(),
this.buildAfterAllHooks()
)
}
}
21 changes: 18 additions & 3 deletions src/SupportCodeLibraryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
DefinedHook,
DefinedParameterType,
DefinedStep,
DefinedTestCaseHook,
DefinedTestRunHook,
MatchedStep,
SupportCodeLibrary,
UndefinedParameterType,
Expand All @@ -15,8 +16,10 @@ export class SupportCodeLibraryImpl implements SupportCodeLibrary {
private readonly parameterTypes: ReadonlyArray<DefinedParameterType> = [],
private readonly steps: ReadonlyArray<DefinedStep> = [],
private readonly undefinedParameterTypes: ReadonlyArray<UndefinedParameterType> = [],
private readonly beforeHooks: ReadonlyArray<DefinedHook> = [],
private readonly afterHooks: ReadonlyArray<DefinedHook> = []
private readonly beforeHooks: ReadonlyArray<DefinedTestCaseHook> = [],
private readonly afterHooks: ReadonlyArray<DefinedTestCaseHook> = [],
private readonly beforeAllHooks: ReadonlyArray<DefinedTestRunHook> = [],
private readonly afterAllHooks: ReadonlyArray<DefinedTestRunHook> = []
) {}

findAllStepsBy(text: string) {
Expand Down Expand Up @@ -51,6 +54,14 @@ export class SupportCodeLibraryImpl implements SupportCodeLibrary {
})
}

getAllBeforeAllHooks(): ReadonlyArray<DefinedTestRunHook> {
return [...this.beforeAllHooks]
}

getAllAfterAllHooks(): ReadonlyArray<DefinedTestRunHook> {
return [...this.afterAllHooks]
}

toEnvelopes() {
return [
...this.parameterTypes.map((parameterType) => ({ parameterType })),
Expand All @@ -60,6 +71,10 @@ export class SupportCodeLibraryImpl implements SupportCodeLibrary {
...this.undefinedParameterTypes.map((undefinedParameterType) => ({ undefinedParameterType })),
...this.beforeHooks.map((definedHook) => definedHook.toMessage()).map((hook) => ({ hook })),
...this.afterHooks.map((definedHook) => definedHook.toMessage()).map((hook) => ({ hook })),
...this.beforeAllHooks
.map((definedHook) => definedHook.toMessage())
.map((hook) => ({ hook })),
...this.afterAllHooks.map((definedHook) => definedHook.toMessage()).map((hook) => ({ hook })),
]
}
}
Loading
Loading