Skip to content

Commit 8d01a0f

Browse files
committed
split some functions out
1 parent 5d2cfdc commit 8d01a0f

File tree

2 files changed

+105
-101
lines changed

2 files changed

+105
-101
lines changed

javascript/src/index.ts

Lines changed: 5 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
import * as assert from 'node:assert'
2-
3-
import { Envelope, TestCaseStarted, TestStepResultStatus } from '@cucumber/messages'
1+
import { Envelope } from '@cucumber/messages'
42
import xmlbuilder from 'xmlbuilder'
53

64
import { ExtendedQuery } from './ExtendedQuery.js'
7-
import { countStatuses, durationToSeconds, formatStep } from './helpers.js'
8-
import {
9-
namingStrategy,
10-
NamingStrategyExampleName,
11-
NamingStrategyFeatureName,
12-
NamingStrategyLength,
13-
} from './Lineage.js'
14-
15-
const NAMING_STRATEGY = namingStrategy(
16-
NamingStrategyLength.LONG,
17-
NamingStrategyFeatureName.EXCLUDE,
18-
NamingStrategyExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED
19-
)
5+
import { makeReport } from './makeReport.js'
206

217
export default {
228
type: 'formatter',
@@ -27,16 +13,16 @@ export default {
2713
on: (type: 'message', handler: (message: Envelope) => void) => void
2814
write: (content: string) => void
2915
}) {
30-
const cucumberQuery = new ExtendedQuery()
16+
const query = new ExtendedQuery()
3117
const builder = xmlbuilder
3218
.create('testsuite', { invalidCharReplacement: '' })
3319
.att('name', 'Cucumber')
3420

3521
on('message', (message) => {
36-
cucumberQuery.update(message)
22+
query.update(message)
3723

3824
if (message.testRunFinished) {
39-
const testSuite = makeReport(cucumberQuery)
25+
const testSuite = makeReport(query)
4026
builder.att('time', testSuite.time)
4127
builder.att('tests', testSuite.tests)
4228
builder.att('skipped', testSuite.skipped)
@@ -69,85 +55,3 @@ export default {
6955
})
7056
},
7157
}
72-
73-
interface ReportSuite {
74-
time: number
75-
tests: number
76-
skipped: number
77-
failures: number
78-
errors: number
79-
testCases: ReadonlyArray<ReportTestCase>
80-
}
81-
82-
interface ReportTestCase {
83-
classname: string
84-
name: string
85-
time: number
86-
failure?: ReportFailure
87-
output: string
88-
}
89-
90-
interface ReportFailure {
91-
kind: 'failure' | 'skipped'
92-
type?: string
93-
message?: string
94-
stack?: string
95-
}
96-
97-
function makeReport(query: ExtendedQuery): ReportSuite {
98-
const statuses = query.countMostSevereTestStepResultStatus()
99-
return {
100-
time: durationToSeconds(query.findTestRunDuration()),
101-
tests: countStatuses(statuses),
102-
skipped: countStatuses(statuses, (status) => status === TestStepResultStatus.SKIPPED),
103-
failures: countStatuses(
104-
statuses,
105-
(status) => status !== TestStepResultStatus.PASSED && status !== TestStepResultStatus.SKIPPED
106-
),
107-
errors: 0,
108-
testCases: makeTestCases(query),
109-
}
110-
}
111-
112-
function makeTestCases(query: ExtendedQuery): ReadonlyArray<ReportTestCase> {
113-
return query.findAllTestCaseStarted().map((testCaseStarted) => {
114-
const pickle = query.findPickleBy(testCaseStarted)
115-
assert.ok(pickle, 'Expected to find Pickle by TestCaseStarted')
116-
const lineage = query.findLineageBy(pickle)
117-
118-
return {
119-
classname: lineage?.feature?.name ?? pickle.uri,
120-
name: query.findNameOf(pickle, NAMING_STRATEGY),
121-
time: durationToSeconds(query.findTestCaseDurationBy(testCaseStarted)),
122-
failure: makeFailure(query, testCaseStarted),
123-
output: query
124-
.findTestStepFinishedAndTestStepBy(testCaseStarted)
125-
// filter out hooks
126-
.filter(([, testStep]) => !!testStep.pickleStepId)
127-
.map(([testStepFinished, testStep]) => {
128-
const pickleStep = query.findPickleStepBy(testStep)
129-
assert.ok(pickleStep, 'Expected to find PickleStep by TestStep')
130-
const gherkinStep = query.findStepBy(pickleStep)
131-
assert.ok(gherkinStep, 'Expected to find Step by PickleStep')
132-
return formatStep(gherkinStep, pickleStep, testStepFinished.testStepResult.status)
133-
})
134-
.join('\n'),
135-
}
136-
})
137-
}
138-
139-
function makeFailure(
140-
query: ExtendedQuery,
141-
testCaseStarted: TestCaseStarted
142-
): ReportFailure | undefined {
143-
const result = query.findMostSevereTestStepResultBy(testCaseStarted)
144-
if (result.status === TestStepResultStatus.PASSED) {
145-
return undefined
146-
}
147-
return {
148-
kind: result.status === TestStepResultStatus.SKIPPED ? 'skipped' : 'failure',
149-
type: result.exception?.type,
150-
message: result.exception?.message,
151-
stack: result.exception?.stackTrace ?? result.message,
152-
}
153-
}

javascript/src/makeReport.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import assert from 'node:assert'
2+
3+
import { TestCaseStarted, TestStepResultStatus } from '@cucumber/messages'
4+
5+
import { ExtendedQuery } from './ExtendedQuery.js'
6+
import { countStatuses, durationToSeconds, formatStep } from './helpers.js'
7+
import {
8+
namingStrategy,
9+
NamingStrategyExampleName,
10+
NamingStrategyFeatureName,
11+
NamingStrategyLength,
12+
} from './Lineage.js'
13+
14+
const NAMING_STRATEGY = namingStrategy(
15+
NamingStrategyLength.LONG,
16+
NamingStrategyFeatureName.EXCLUDE,
17+
NamingStrategyExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED
18+
)
19+
20+
interface ReportSuite {
21+
time: number
22+
tests: number
23+
skipped: number
24+
failures: number
25+
errors: number
26+
testCases: ReadonlyArray<ReportTestCase>
27+
}
28+
29+
interface ReportTestCase {
30+
classname: string
31+
name: string
32+
time: number
33+
failure?: ReportFailure
34+
output: string
35+
}
36+
37+
interface ReportFailure {
38+
kind: 'failure' | 'skipped'
39+
type?: string
40+
message?: string
41+
stack?: string
42+
}
43+
44+
export function makeReport(query: ExtendedQuery): ReportSuite {
45+
const statuses = query.countMostSevereTestStepResultStatus()
46+
return {
47+
time: durationToSeconds(query.findTestRunDuration()),
48+
tests: countStatuses(statuses),
49+
skipped: countStatuses(statuses, (status) => status === TestStepResultStatus.SKIPPED),
50+
failures: countStatuses(
51+
statuses,
52+
(status) => status !== TestStepResultStatus.PASSED && status !== TestStepResultStatus.SKIPPED
53+
),
54+
errors: 0,
55+
testCases: makeTestCases(query),
56+
}
57+
}
58+
59+
function makeTestCases(query: ExtendedQuery): ReadonlyArray<ReportTestCase> {
60+
return query.findAllTestCaseStarted().map((testCaseStarted) => {
61+
const pickle = query.findPickleBy(testCaseStarted)
62+
assert.ok(pickle, 'Expected to find Pickle by TestCaseStarted')
63+
const lineage = query.findLineageBy(pickle)
64+
65+
return {
66+
classname: lineage?.feature?.name ?? pickle.uri,
67+
name: query.findNameOf(pickle, NAMING_STRATEGY),
68+
time: durationToSeconds(query.findTestCaseDurationBy(testCaseStarted)),
69+
failure: makeFailure(query, testCaseStarted),
70+
output: query
71+
.findTestStepFinishedAndTestStepBy(testCaseStarted)
72+
// filter out hooks
73+
.filter(([, testStep]) => !!testStep.pickleStepId)
74+
.map(([testStepFinished, testStep]) => {
75+
const pickleStep = query.findPickleStepBy(testStep)
76+
assert.ok(pickleStep, 'Expected to find PickleStep by TestStep')
77+
const gherkinStep = query.findStepBy(pickleStep)
78+
assert.ok(gherkinStep, 'Expected to find Step by PickleStep')
79+
return formatStep(gherkinStep, pickleStep, testStepFinished.testStepResult.status)
80+
})
81+
.join('\n'),
82+
}
83+
})
84+
}
85+
86+
function makeFailure(
87+
query: ExtendedQuery,
88+
testCaseStarted: TestCaseStarted
89+
): ReportFailure | undefined {
90+
const result = query.findMostSevereTestStepResultBy(testCaseStarted)
91+
if (result.status === TestStepResultStatus.PASSED) {
92+
return undefined
93+
}
94+
return {
95+
kind: result.status === TestStepResultStatus.SKIPPED ? 'skipped' : 'failure',
96+
type: result.exception?.type,
97+
message: result.exception?.message,
98+
stack: result.exception?.stackTrace ?? result.message,
99+
}
100+
}

0 commit comments

Comments
 (0)