From 6b45eeba517fd47640fc988277c9093dda97219d Mon Sep 17 00:00:00 2001 From: Ryan Miller Date: Thu, 29 Aug 2024 15:43:05 +1000 Subject: [PATCH] chore: add fuzzy ms case to timed log test --- ts/test/session/unit/utils/loggerTimed_test.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ts/test/session/unit/utils/loggerTimed_test.ts b/ts/test/session/unit/utils/loggerTimed_test.ts index 7e065f042..18d441808 100644 --- a/ts/test/session/unit/utils/loggerTimed_test.ts +++ b/ts/test/session/unit/utils/loggerTimed_test.ts @@ -7,9 +7,16 @@ const ms = 'ms'; type TimePair = { offset: number; output: string }; type TimePairs = Readonly>; -function testPair(pair: TimePair) { - const result = TimedLog.formatDistanceToNow(Date.now() - pair.offset); - assert.strictEqual(result, pair.output); +function testPair({ offset, output }: TimePair) { + const result = TimedLog.formatDistanceToNow(Date.now() - offset); + assert.strictEqual(result, output); +} + +function testPairFuzzy({ offset, output }: TimePair) { + const result = TimedLog.formatDistanceToNow(Date.now() - offset); + const resultNumber = parseInt(result.replaceAll(/[a-zA-Z]/g, ''), 10); + const expectedNumber = parseInt(output.replaceAll(/[a-zA-Z]/g, ''), 10); + assert.approximately(resultNumber, expectedNumber, 1); } describe('TimedLog', () => { @@ -25,7 +32,7 @@ describe('TimedLog', () => { { offset: 555, output: `555${ms}` }, { offset: 900, output: `900${ms}` }, ] satisfies TimePairs - ).forEach(testPair); + ).forEach(testPairFuzzy); }); it('should not round milliseconds when the time difference is less than 1 second', () => { @@ -38,7 +45,7 @@ describe('TimedLog', () => { { offset: 998, output: `998${ms}` }, { offset: 999, output: `999${ms}` }, ] satisfies TimePairs - ).forEach(testPair); + ).forEach(testPairFuzzy); }); it('should return exact seconds when the time difference is an exact second', () => {