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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ 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 `pickleStep` to `UndefinedError` ([#13](https://github.com/cucumber/javascript-core/pull/13))
- Add `getExpressionGenerator()` to `SupportCodeLibrary` ([#13](https://github.com/cucumber/javascript-core/pull/13))

## [0.4.1] - 2025-09-05
### Fixed
Expand Down
7 changes: 6 additions & 1 deletion cucumber-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

import { Argument } from '@cucumber/cucumber-expressions';
import { CucumberExpression } from '@cucumber/cucumber-expressions';
import { CucumberExpressionGenerator } from '@cucumber/cucumber-expressions';
import { Envelope } from '@cucumber/messages';
import { GherkinDocument } from '@cucumber/messages';
import { Hook } from '@cucumber/messages';
import { IdGenerator } from '@cucumber/messages';
import { NamingStrategy } from '@cucumber/query';
import parse from '@cucumber/tag-expressions';
import { Pickle } from '@cucumber/messages';
import { PickleStep } from '@cucumber/messages';
import { RegularExpression } from '@cucumber/cucumber-expressions';
import { SourceReference } from '@cucumber/messages';
import { StepDefinition } from '@cucumber/messages';
Expand Down Expand Up @@ -179,6 +181,7 @@ export interface SupportCodeLibrary {
findAllStepsBy(text: string): ReadonlyArray<MatchedStep>;
getAllAfterAllHooks(): ReadonlyArray<DefinedTestRunHook>;
getAllBeforeAllHooks(): ReadonlyArray<DefinedTestRunHook>;
getExpressionGenerator(): CucumberExpressionGenerator;
toEnvelopes(): ReadonlyArray<Envelope>;
}

Expand All @@ -203,7 +206,9 @@ export interface TestPlanOptions {

// @public
export class UndefinedError extends Error {
constructor(text: string);
constructor(pickleStep: PickleStep);
// (undocumented)
readonly pickleStep: PickleStep;
}

// @public
Expand Down
1 change: 1 addition & 0 deletions src/SupportCodeBuilderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export class SupportCodeBuilderImpl implements SupportCodeBuilder {

build() {
return new SupportCodeLibraryImpl(
this.parameterTypeRegistry,
this.buildParameterTypes(),
this.buildSteps(),
this.buildUndefinedParameterTypes(),
Expand Down
7 changes: 7 additions & 0 deletions src/SupportCodeLibraryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CucumberExpressionGenerator, ParameterTypeRegistry } from '@cucumber/cucumber-expressions'

import {
DefinedParameterType,
DefinedStep,
Expand All @@ -13,6 +15,7 @@ import {
*/
export class SupportCodeLibraryImpl implements SupportCodeLibrary {
constructor(
private readonly parameterTypeRegistry: ParameterTypeRegistry,
private readonly parameterTypes: ReadonlyArray<DefinedParameterType> = [],
private readonly steps: ReadonlyArray<DefinedStep> = [],
private readonly undefinedParameterTypes: ReadonlyArray<UndefinedParameterType> = [],
Expand All @@ -36,6 +39,10 @@ export class SupportCodeLibraryImpl implements SupportCodeLibrary {
return results
}

getExpressionGenerator(): CucumberExpressionGenerator {
return new CucumberExpressionGenerator(() => this.parameterTypeRegistry.parameterTypes)
}

findAllBeforeHooksBy(tags: ReadonlyArray<string>) {
return this.beforeHooks.filter((def) => {
if (def.tags) {
Expand Down
6 changes: 4 additions & 2 deletions src/UndefinedError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { PickleStep } from '@cucumber/messages'

/**
* Represents an error that occurs when no step definitions are found matching the text of a step
* @public
*/
export class UndefinedError extends Error {
constructor(text: string) {
super(`No matching step definitions found for text "${text}"`)
constructor(public readonly pickleStep: PickleStep) {
super(`No matching step definitions found for text "${pickleStep.text}"`)
}
}
9 changes: 9 additions & 0 deletions src/buildSupportCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ describe('buildSupportCode', () => {
})
})

describe('expression generator', () => {
it('returns a cucumber expression generator primed with the parameter type registry', () => {
const library = buildSupportCode({ newId }).build()
const expressionGenerator = library.getExpressionGenerator()
const expressions = expressionGenerator.generateExpressions('I have 17 cukes in my belly')
expect(expressions.length).to.eq(2)
})
})

describe('test run hooks', () => {
let library: SupportCodeLibrary
beforeEach(() => {
Expand Down
7 changes: 6 additions & 1 deletion src/makeTestPlan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ describe('makeTestPlan', () => {
}
)

expect(() => result.testCases[0].testSteps[0].prepare(undefined)).to.throw(UndefinedError)
try {
result.testCases[0].testSteps[0].prepare(undefined)
} catch (err: any) {
expect(err).to.be.instanceOf(UndefinedError)
expect(err.pickleStep).to.eq(pickles[0].steps[0])
}
})

it('matches and prepares a step without parameters', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/makeTestPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function fromPickleSteps(
always: false,
prepare(thisArg) {
if (matched.length < 1) {
throw new UndefinedError(pickleStep.text)
throw new UndefinedError(pickleStep)
} else if (matched.length > 1) {
throw new AmbiguousError(
pickleStep.text,
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Argument, CucumberExpression, RegularExpression } from '@cucumber/cucumber-expressions'
import {
Argument,
CucumberExpression,
CucumberExpressionGenerator,
RegularExpression,
} from '@cucumber/cucumber-expressions'
import {
Envelope,
GherkinDocument,
Expand Down Expand Up @@ -329,6 +334,10 @@ export interface SupportCodeLibrary {
* Find all step definitions whose expression is a match for the given text
*/
findAllStepsBy(text: string): ReadonlyArray<MatchedStep>
/**
* Get a generator for Cucumber Expressions based on the currently defined parameter types
*/
getExpressionGenerator(): CucumberExpressionGenerator
/**
* Get all BeforeAll hooks
*/
Expand Down
Loading