Skip to content

Commit

Permalink
fix: ensure durations are integers in json formatter (#2094)
Browse files Browse the repository at this point in the history
* fix: ensure durations are integers in json formatter

* update CHANGELOG.md
  • Loading branch information
davidjgoss committed Jul 28, 2022
1 parent fb3144e commit d547f78
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,4 @@

# Changelog

All notable changes to this project will be documented in this file.
Expand All @@ -8,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CONTRIBUTING.md) on how to contribute to Cucumber.

## [Unreleased]
### Fixed
- Ensure durations are integers in JSON formatter ([#2094](https://github.com/cucumber/cucumber-js/pull/2094))

## [8.5.0] - 2022-07-19
### Changed
Expand Down
7 changes: 7 additions & 0 deletions src/formatter/helpers/duration_helpers.ts
@@ -0,0 +1,7 @@
import { Duration } from '@cucumber/messages'

const NANOS_IN_SECOND = 1_000_000_000

export function durationToNanoseconds(duration: Duration): number {
return Math.floor(duration.seconds * NANOS_IN_SECOND + duration.nanos)
}
18 changes: 18 additions & 0 deletions src/formatter/helpers/duration_helpers_spec.ts
@@ -0,0 +1,18 @@
import { expect } from 'chai'
import { durationToNanoseconds } from './duration_helpers'

describe('duration helpers', () => {
describe('durationToNanoseconds', () => {
it('should convert under a second', () => {
expect(durationToNanoseconds({ seconds: 0, nanos: 257344166 })).to.eq(
257344166
)
})

it('should convert over a second', () => {
expect(durationToNanoseconds({ seconds: 2, nanos: 1043459 })).to.eq(
2001043459
)
})
})
})
6 changes: 2 additions & 4 deletions src/formatter/json_formatter.ts
Expand Up @@ -8,6 +8,7 @@ import {
import { ITestCaseAttempt } from './helpers/event_data_collector'
import { doesHaveValue, doesNotHaveValue } from '../value_checker'
import { parseStepArgument } from '../step_arguments'
import { durationToNanoseconds } from './helpers/duration_helpers'

const { getGherkinStepMap, getGherkinScenarioMap } = GherkinDocumentParser

Expand Down Expand Up @@ -280,10 +281,7 @@ export default class JsonFormatter extends Formatter {
status: messages.TestStepResultStatus[status].toLowerCase(),
}
if (doesHaveValue(testStepResult.duration)) {
data.result.duration =
messages.TimeConversion.durationToMilliseconds(
testStepResult.duration
) * 1000000
data.result.duration = durationToNanoseconds(testStepResult.duration)
}
if (
status === messages.TestStepResultStatus.FAILED &&
Expand Down

0 comments on commit d547f78

Please sign in to comment.