Skip to content

Commit 308bcb3

Browse files
committed
fix(tests): add retry logic for flaky performance timing test
Fix flaky "should measure elapsed time" test that was failing due to timer imprecision. setTimeout(10) does not guarantee exactly 10ms or more will elapse due to OS scheduling and timer resolution. Changes: - Lower threshold from 10ms to 9ms to account for timer imprecision - Add retry: 3 option to automatically retry the test on failure - Add explanatory comment about setTimeout behavior This test was failing intermittently with elapsed times like 9.775ms, which is less than the 10ms threshold but within normal timer variance.
1 parent 5723c70 commit 308bcb3

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

test/performance.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ describe('performance', () => {
2020
})
2121

2222
describe('basic performance measurements', () => {
23-
it('should measure elapsed time', async () => {
24-
const start = performance.now()
25-
await new Promise(resolve => setTimeout(resolve, 10))
26-
const end = performance.now()
27-
const elapsed = end - start
28-
expect(elapsed).toBeGreaterThan(0)
29-
expect(elapsed).toBeGreaterThanOrEqual(10)
30-
})
23+
it(
24+
'should measure elapsed time',
25+
{ retry: 3 },
26+
async () => {
27+
const start = performance.now()
28+
await new Promise(resolve => setTimeout(resolve, 10))
29+
const end = performance.now()
30+
const elapsed = end - start
31+
expect(elapsed).toBeGreaterThan(0)
32+
// Allow for timer imprecision (9ms threshold instead of 10ms)
33+
// setTimeout is not guaranteed to be exact due to OS scheduling
34+
expect(elapsed).toBeGreaterThanOrEqual(9)
35+
},
36+
)
3137

3238
it('should support performance.now()', () => {
3339
const now = performance.now()

0 commit comments

Comments
 (0)