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 `getAllSources()` method to `SupportCodeLibrary` ([#34r](https://github.com/cucumber/javascript-core/pull/34))

## [0.8.0] - 2025-11-25
### Changed
Expand Down
1 change: 1 addition & 0 deletions cucumber-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export interface SupportCodeLibrary {
findAllStepsBy(text: string): ReadonlyArray<MatchedStep>;
getAllAfterAllHooks(): ReadonlyArray<DefinedTestRunHook>;
getAllBeforeAllHooks(): ReadonlyArray<DefinedTestRunHook>;
getAllSources(): ReadonlyArray<SourceReference>;
getExpressionGenerator(): CucumberExpressionGenerator;
toEnvelopes(): ReadonlyArray<Envelope>;
}
Expand Down
12 changes: 12 additions & 0 deletions src/SupportCodeLibraryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CucumberExpressionGenerator, ParameterTypeRegistry } from '@cucumber/cucumber-expressions'
import { SourceReference } from '@cucumber/messages'

import {
DefinedParameterType,
Expand Down Expand Up @@ -61,6 +62,17 @@ export class SupportCodeLibraryImpl implements SupportCodeLibrary {
})
}

getAllSources(): ReadonlyArray<SourceReference> {
return [
...this.parameterTypes.map((pt) => pt.sourceReference),
...this.steps.map((step) => step.sourceReference),
...this.beforeHooks.map((hook) => hook.sourceReference),
...this.afterHooks.map((hook) => hook.sourceReference),
...this.beforeAllHooks.map((hook) => hook.sourceReference),
...this.afterAllHooks.map((hook) => hook.sourceReference),
]
}

getAllBeforeAllHooks(): ReadonlyArray<DefinedTestRunHook> {
return [...this.beforeAllHooks]
}
Expand Down
55 changes: 53 additions & 2 deletions src/buildSupportCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('buildSupportCode', () => {
describe('test case hooks', () => {
let library: SupportCodeLibrary
beforeEach(() => {
library = buildSupportCode({ newId: IdGenerator.incrementing() })
library = buildSupportCode({ newId })
.beforeHook({
name: 'general setup',
fn: sinon.stub(),
Expand Down Expand Up @@ -348,7 +348,7 @@ describe('buildSupportCode', () => {
describe('test run hooks', () => {
let library: SupportCodeLibrary
beforeEach(() => {
library = buildSupportCode({ newId: IdGenerator.incrementing() })
library = buildSupportCode({ newId })
.beforeAllHook({
name: 'setup 1',
fn: sinon.stub(),
Expand Down Expand Up @@ -423,4 +423,55 @@ describe('buildSupportCode', () => {
])
})
})

describe('sources', () => {
it('should return all source references from parameter types, steps, and hooks', () => {
const library = buildSupportCode({ newId })
.parameterType({
name: 'custom',
regexp: /\d+/,
sourceReference: { uri: 'params.ts', location: { line: 1, column: 1 } },
})
.step({
pattern: 'a step',
fn: sinon.stub(),
sourceReference: { uri: 'steps.js', location: { line: 1, column: 1 } },
})
.beforeHook({
fn: sinon.stub(),
sourceReference: { uri: 'hooks.ts', location: { line: 2, column: 1 } },
})
.afterHook({
fn: sinon.stub(),
sourceReference: { uri: 'hooks.ts', location: { line: 3, column: 1 } },
})
.beforeAllHook({
fn: sinon.stub(),
sourceReference: { uri: 'setup.js', location: { line: 1, column: 1 } },
})
.afterAllHook({
fn: sinon.stub(),
sourceReference: { uri: 'teardown.js', location: { line: 1, column: 1 } },
})
.build()

const sources = library.getAllSources()

expect(sources).to.have.lengthOf(6)
expect(sources).to.deep.include({ uri: 'params.ts', location: { line: 1, column: 1 } })
expect(sources).to.deep.include({ uri: 'steps.js', location: { line: 1, column: 1 } })
expect(sources).to.deep.include({ uri: 'hooks.ts', location: { line: 2, column: 1 } })
expect(sources).to.deep.include({ uri: 'hooks.ts', location: { line: 3, column: 1 } })
expect(sources).to.deep.include({ uri: 'setup.js', location: { line: 1, column: 1 } })
expect(sources).to.deep.include({ uri: 'teardown.js', location: { line: 1, column: 1 } })
})

it('should return empty array when there is no support code', () => {
const library = buildSupportCode({ newId }).build()

const sources = library.getAllSources()

expect(sources).to.deep.equal([])
})
})
})
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ export interface SupportCodeLibrary {
* Find all step definitions whose expression is a match for the given text
*/
findAllStepsBy(text: string): ReadonlyArray<MatchedStep>
/**
* Get all source references from across all support code
*/
getAllSources(): ReadonlyArray<SourceReference>
/**
* Get a generator for Cucumber Expressions based on the currently defined parameter types
*/
Expand Down