Skip to content

Commit

Permalink
chore: add fuzzy ms case to timed log test
Browse files Browse the repository at this point in the history
  • Loading branch information
Aerilym committed Aug 29, 2024
1 parent b990cd9 commit eeb778c
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ts/test/session/unit/utils/loggerTimed_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ const ms = 'ms';
type TimePair = { offset: number; output: string };
type TimePairs = Readonly<Array<TimePair>>;

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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit eeb778c

Please sign in to comment.