Skip to content

Commit 35730c4

Browse files
committed
fix: ensure durations are integers in json formatter
1 parent fb3144e commit 35730c4

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Duration } from '@cucumber/messages'
2+
3+
const NANOS_IN_SECOND = 1_000_000_000
4+
5+
export function durationToNanoseconds(duration: Duration): number {
6+
return Math.floor(duration.seconds * NANOS_IN_SECOND + duration.nanos)
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from 'chai'
2+
import { durationToNanoseconds } from './duration_helpers'
3+
4+
describe('duration helpers', () => {
5+
describe('durationToNanoseconds', () => {
6+
it('should convert under a second', () => {
7+
expect(durationToNanoseconds({ seconds: 0, nanos: 257344166 })).to.eq(
8+
257344166
9+
)
10+
})
11+
12+
it('should convert over a second', () => {
13+
expect(durationToNanoseconds({ seconds: 2, nanos: 1043459 })).to.eq(
14+
2001043459
15+
)
16+
})
17+
})
18+
})

src/formatter/json_formatter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { ITestCaseAttempt } from './helpers/event_data_collector'
99
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
1010
import { parseStepArgument } from '../step_arguments'
11+
import { durationToNanoseconds } from './helpers/duration_helpers'
1112

1213
const { getGherkinStepMap, getGherkinScenarioMap } = GherkinDocumentParser
1314

@@ -280,10 +281,7 @@ export default class JsonFormatter extends Formatter {
280281
status: messages.TestStepResultStatus[status].toLowerCase(),
281282
}
282283
if (doesHaveValue(testStepResult.duration)) {
283-
data.result.duration =
284-
messages.TimeConversion.durationToMilliseconds(
285-
testStepResult.duration
286-
) * 1000000
284+
data.result.duration = durationToNanoseconds(testStepResult.duration)
287285
}
288286
if (
289287
status === messages.TestStepResultStatus.FAILED &&

0 commit comments

Comments
 (0)