Skip to content

Commit 4060fe3

Browse files
committed
fix: improve type safety in formatter_output_helpers
1 parent ff894c1 commit 4060fe3

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

features/support/formatter_output_helpers.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
IJsonStep,
1111
} from '../../src/formatter/json_formatter'
1212

13+
function isObject(obj: unknown): obj is Record<string, unknown> {
14+
return typeof obj === 'object' && obj !== null
15+
}
16+
1317
// Converting windows stack trace to posix and removing cwd
1418
// C:\\project\\path\\features\\support/code.js
1519
// becomes
@@ -22,37 +26,46 @@ function normalizeExceptionAndUri(exception: string, cwd: string): string {
2226
.split('\n')[0]
2327
}
2428

25-
function normalizeMessage(obj: any, cwd: string): void {
26-
if (doesHaveValue(obj.uri)) {
27-
obj.uri = normalizeExceptionAndUri(obj.uri, cwd)
28-
}
29-
if (doesHaveValue(obj.sourceReference?.uri)) {
30-
obj.sourceReference.uri = normalizeExceptionAndUri(
31-
obj.sourceReference.uri,
32-
cwd
33-
)
34-
}
35-
if (doesHaveValue(obj.testStepResult)) {
36-
if (doesHaveValue(obj.testStepResult.duration)) {
37-
obj.testStepResult.duration.nanos = 0
29+
function normalizeMessage(
30+
obj: messages.Envelope[keyof messages.Envelope],
31+
cwd: string
32+
): void {
33+
if (isObject(obj)) {
34+
if (typeof obj.uri === 'string') {
35+
obj.uri = normalizeExceptionAndUri(obj.uri, cwd)
3836
}
39-
if (doesHaveValue(obj.testStepResult.message)) {
40-
obj.testStepResult.message = normalizeExceptionAndUri(
41-
obj.testStepResult.message,
37+
if (
38+
isObject(obj.sourceReference) &&
39+
typeof obj.sourceReference.uri === 'string'
40+
) {
41+
obj.sourceReference.uri = normalizeExceptionAndUri(
42+
obj.sourceReference.uri,
4243
cwd
4344
)
4445
}
46+
if (isObject(obj.testStepResult)) {
47+
if (isObject(obj.testStepResult.duration)) {
48+
obj.testStepResult.duration.nanos = 0
49+
}
50+
if (typeof obj.testStepResult.message === 'string') {
51+
obj.testStepResult.message = normalizeExceptionAndUri(
52+
obj.testStepResult.message,
53+
cwd
54+
)
55+
}
56+
}
4557
}
4658
}
4759

4860
export function normalizeMessageOutput(
4961
envelopeObjects: messages.Envelope[],
5062
cwd: string
5163
): messages.Envelope[] {
52-
envelopeObjects.forEach((e: any) => {
53-
for (const key in e) {
64+
envelopeObjects.forEach((e: messages.Envelope) => {
65+
const keys = Object.keys(e) as (keyof messages.Envelope)[]
66+
keys.forEach((key) => {
5467
normalizeMessage(e[key], cwd)
55-
}
68+
})
5669
})
5770
return envelopeObjects
5871
}

0 commit comments

Comments
 (0)