Skip to content

Commit c66b1ab

Browse files
committed
Js implementation
1 parent 7de60e8 commit c66b1ab

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

javascript/package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/src/index.spec.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import util from 'node:util'
55

66
import { NdjsonToMessageStream } from '@cucumber/message-streams'
77
import { Envelope } from '@cucumber/messages'
8+
import { namingStrategy, NamingStrategyLength } from '@cucumber/query'
89
import { expect, use } from 'chai'
910
import chaiXml from 'chai-xml'
1011
import { globbySync } from 'globby'
@@ -22,13 +23,33 @@ describe('Acceptance Tests', async function () {
2223
absolute: true,
2324
})
2425

25-
for (const ndjsonFile of ndjsonFiles) {
26+
const testCases = ndjsonFiles.map((ndjsonFile) => {
2627
const [suiteName] = path.basename(ndjsonFile).split('.')
27-
it(suiteName, async () => {
28+
return {
29+
suiteName,
30+
source: ndjsonFile,
31+
strategyName: 'default',
32+
options: {},
33+
}
34+
})
35+
36+
testCases.push({
37+
suiteName: 'examples-tables',
38+
source: '../testdata/src/examples-tables.ndjson',
39+
strategyName: 'custom',
40+
options: {
41+
suiteName: 'Cucumber Suite',
42+
testClassName: 'Cucumber Class',
43+
testNamingStrategy: namingStrategy(NamingStrategyLength.LONG),
44+
},
45+
})
46+
47+
for (const testCase of testCases) {
48+
it(testCase.suiteName + ' -> ' + testCase.strategyName, async () => {
2849
let emit: (message: Envelope) => void
2950
let content = ''
3051
formatter.formatter({
31-
options: {},
52+
options: testCase.options,
3253
on(type, handler) {
3354
emit = handler
3455
},
@@ -38,7 +59,7 @@ describe('Acceptance Tests', async function () {
3859
})
3960

4061
await asyncPipeline(
41-
fs.createReadStream(ndjsonFile, { encoding: 'utf-8' }),
62+
fs.createReadStream(testCase.source, { encoding: 'utf-8' }),
4263
new NdjsonToMessageStream(),
4364
new Writable({
4465
objectMode: true,
@@ -49,9 +70,12 @@ describe('Acceptance Tests', async function () {
4970
})
5071
)
5172

52-
const expectedXml = fs.readFileSync(ndjsonFile.replace('.ndjson', '.xml'), {
53-
encoding: 'utf-8',
54-
})
73+
const expectedXml = fs.readFileSync(
74+
testCase.source.replace('.ndjson', '.' + testCase.strategyName + '.xml'),
75+
{
76+
encoding: 'utf-8',
77+
}
78+
)
5579
expect(content).xml.to.be.valid()
5680
expect(content).xml.to.deep.eq(expectedXml)
5781
})

javascript/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Envelope } from '@cucumber/messages'
2-
import { Query } from '@cucumber/query'
2+
import { NamingStrategy, Query } from '@cucumber/query'
33
import xmlbuilder from 'xmlbuilder'
44

55
import { makeReport } from './makeReport.js'
@@ -11,7 +11,11 @@ export default {
1111
on,
1212
write,
1313
}: {
14-
options: { suiteName?: string }
14+
options: {
15+
suiteName?: string
16+
testClassName?: string
17+
testNamingStrategy?: NamingStrategy
18+
}
1519
on: (type: 'message', handler: (message: Envelope) => void) => void
1620
write: (content: string) => void
1721
}) {
@@ -24,7 +28,7 @@ export default {
2428
query.update(message)
2529

2630
if (message.testRunFinished) {
27-
const testSuite = makeReport(query)
31+
const testSuite = makeReport(query, options.testClassName, options.testNamingStrategy)
2832
builder.att('time', testSuite.time)
2933
builder.att('tests', testSuite.tests)
3034
builder.att('skipped', testSuite.skipped)

javascript/src/makeReport.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TestCaseStarted, TestStepResultStatus } from '@cucumber/messages'
22
import {
3+
NamingStrategy,
34
namingStrategy,
45
NamingStrategyExampleName,
56
NamingStrategyFeatureName,
@@ -40,7 +41,11 @@ interface ReportFailure {
4041
stack?: string
4142
}
4243

43-
export function makeReport(query: Query, customNamingStrategy: NamingStrategy = NAMING_STRATEGY): ReportSuite {
44+
export function makeReport(
45+
query: Query,
46+
testClassName: string | undefined = undefined,
47+
customNamingStrategy: NamingStrategy = NAMING_STRATEGY
48+
): ReportSuite {
4449
const statuses = query.countMostSevereTestStepResultStatus()
4550
return {
4651
time: durationToSeconds(query.findTestRunDuration()),
@@ -51,12 +56,16 @@ export function makeReport(query: Query, customNamingStrategy: NamingStrategy =
5156
(status) => status !== TestStepResultStatus.PASSED && status !== TestStepResultStatus.SKIPPED
5257
),
5358
errors: 0,
54-
testCases: makeTestCases(query, customNamingStrategy),
59+
testCases: makeTestCases(query, testClassName, customNamingStrategy),
5560
timestamp: formatTimestamp(query.findTestRunStarted()),
5661
}
5762
}
5863

59-
function makeTestCases(query: Query, namingStrategy: NamingStrategy): ReadonlyArray<ReportTestCase> {
64+
function makeTestCases(
65+
query: Query,
66+
testClassName: string | undefined,
67+
testNamingStrategy: NamingStrategy
68+
): ReadonlyArray<ReportTestCase> {
6069
return query.findAllTestCaseStarted().map((testCaseStarted) => {
6170
const pickle = ensure(
6271
query.findPickleBy(testCaseStarted),
@@ -65,8 +74,8 @@ function makeTestCases(query: Query, namingStrategy: NamingStrategy): ReadonlyAr
6574
const lineage = ensure(query.findLineageBy(pickle), 'Expected to find Lineage by Pickle')
6675

6776
return {
68-
classname: lineage.feature?.name ?? pickle.uri,
69-
name: NAMING_STRATEGY.reduce(lineage, pickle),
77+
classname: testClassName ?? lineage.feature?.name ?? pickle.uri,
78+
name: testNamingStrategy.reduce(lineage, pickle),
7079
time: durationToSeconds(query.findTestCaseDurationBy(testCaseStarted)),
7180
failure: makeFailure(query, testCaseStarted),
7281
output: query

0 commit comments

Comments
 (0)