diff --git a/CHANGELOG.md b/CHANGELOG.md index 35fb9d3ea..419bf1750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ + # Changelog All notable changes to this project will be documented in this file. @@ -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 diff --git a/src/formatter/helpers/duration_helpers.ts b/src/formatter/helpers/duration_helpers.ts new file mode 100644 index 000000000..c3296aedd --- /dev/null +++ b/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) +} diff --git a/src/formatter/helpers/duration_helpers_spec.ts b/src/formatter/helpers/duration_helpers_spec.ts new file mode 100644 index 000000000..bfa2ecc41 --- /dev/null +++ b/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 + ) + }) + }) +}) diff --git a/src/formatter/json_formatter.ts b/src/formatter/json_formatter.ts index 1f758cc05..01544f3d0 100644 --- a/src/formatter/json_formatter.ts +++ b/src/formatter/json_formatter.ts @@ -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 @@ -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 &&