Skip to content

Fix: Improve type safety in features/support/formatter_output_helpers, removes explicit any #1648 #2558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Please see [CONTRIBUTING.md](./CONTRIBUTING.md) on how to contribute to Cucumber
## [Unreleased]
### Changed
- Improve error handling in publish plugin ([#2526](https://github.com/cucumber/cucumber-js/pull/2526))
- Improve type safety in features/support/formatter_output_helpers, removes explicit `any` ([#1648](https://github.com/cucumber/cucumber-js/pull/2106))

## [11.2.0] - 2025-01-09
### Added
Expand Down
51 changes: 32 additions & 19 deletions features/support/formatter_output_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
IJsonStep,
} from '../../src/formatter/json_formatter'

function isObject(obj: unknown): obj is Record<string, unknown> {
return typeof obj === 'object' && obj !== null
}

// Converting windows stack trace to posix and removing cwd
// C:\\project\\path\\features\\support/code.js
// becomes
Expand All @@ -22,37 +26,46 @@ function normalizeExceptionAndUri(exception: string, cwd: string): string {
.split('\n')[0]
}

function normalizeMessage(obj: any, cwd: string): void {
if (doesHaveValue(obj.uri)) {
obj.uri = normalizeExceptionAndUri(obj.uri, cwd)
}
if (doesHaveValue(obj.sourceReference?.uri)) {
obj.sourceReference.uri = normalizeExceptionAndUri(
obj.sourceReference.uri,
cwd
)
}
if (doesHaveValue(obj.testStepResult)) {
if (doesHaveValue(obj.testStepResult.duration)) {
obj.testStepResult.duration.nanos = 0
function normalizeMessage(
obj: messages.Envelope[keyof messages.Envelope],
cwd: string
): void {
if (isObject(obj)) {
if (typeof obj.uri === 'string') {
obj.uri = normalizeExceptionAndUri(obj.uri, cwd)
}
if (doesHaveValue(obj.testStepResult.message)) {
obj.testStepResult.message = normalizeExceptionAndUri(
obj.testStepResult.message,
if (
isObject(obj.sourceReference) &&
typeof obj.sourceReference.uri === 'string'
) {
obj.sourceReference.uri = normalizeExceptionAndUri(
obj.sourceReference.uri,
cwd
)
}
if (isObject(obj.testStepResult)) {
if (isObject(obj.testStepResult.duration)) {
obj.testStepResult.duration.nanos = 0
}
if (typeof obj.testStepResult.message === 'string') {
obj.testStepResult.message = normalizeExceptionAndUri(
obj.testStepResult.message,
cwd
)
}
}
}
}

export function normalizeMessageOutput(
envelopeObjects: messages.Envelope[],
cwd: string
): messages.Envelope[] {
envelopeObjects.forEach((e: any) => {
for (const key in e) {
envelopeObjects.forEach((e: messages.Envelope) => {
const keys = Object.keys(e) as (keyof messages.Envelope)[]
keys.forEach((key) => {
normalizeMessage(e[key], cwd)
}
})
})
return envelopeObjects
}
Expand Down