diff --git a/CHANGELOG.md b/CHANGELOG.md index eb975b5..e4d72ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cucumber-core.api.md b/cucumber-core.api.md index 2241a32..b66b0bf 100644 --- a/cucumber-core.api.md +++ b/cucumber-core.api.md @@ -196,6 +196,7 @@ export interface SupportCodeLibrary { findAllStepsBy(text: string): ReadonlyArray; getAllAfterAllHooks(): ReadonlyArray; getAllBeforeAllHooks(): ReadonlyArray; + getAllSources(): ReadonlyArray; getExpressionGenerator(): CucumberExpressionGenerator; toEnvelopes(): ReadonlyArray; } diff --git a/src/SupportCodeLibraryImpl.ts b/src/SupportCodeLibraryImpl.ts index 20d5c14..88e92ef 100644 --- a/src/SupportCodeLibraryImpl.ts +++ b/src/SupportCodeLibraryImpl.ts @@ -1,4 +1,5 @@ import { CucumberExpressionGenerator, ParameterTypeRegistry } from '@cucumber/cucumber-expressions' +import { SourceReference } from '@cucumber/messages' import { DefinedParameterType, @@ -61,6 +62,17 @@ export class SupportCodeLibraryImpl implements SupportCodeLibrary { }) } + getAllSources(): ReadonlyArray { + 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 { return [...this.beforeAllHooks] } diff --git a/src/buildSupportCode.spec.ts b/src/buildSupportCode.spec.ts index 9970c0a..2a316db 100644 --- a/src/buildSupportCode.spec.ts +++ b/src/buildSupportCode.spec.ts @@ -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(), @@ -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(), @@ -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([]) + }) + }) }) diff --git a/src/types.ts b/src/types.ts index 1741e45..b21d5cb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -336,6 +336,10 @@ export interface SupportCodeLibrary { * Find all step definitions whose expression is a match for the given text */ findAllStepsBy(text: string): ReadonlyArray + /** + * Get all source references from across all support code + */ + getAllSources(): ReadonlyArray /** * Get a generator for Cucumber Expressions based on the currently defined parameter types */