Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure durations are integers in json formatter #2094

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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