test: fix flaky sleep test (#571)

This commit is contained in:
Samuel Gunter
2025-03-24 02:28:41 -05:00
committed by GitHub
parent fa9f78b46e
commit 5cd56259f7

View File

@@ -3,11 +3,13 @@ import { describe, expect, it } from 'vitest';
describe('sleep', () => { describe('sleep', () => {
it('should resolve after the specified number of milliseconds', async () => { it('should resolve after the specified number of milliseconds', async () => {
const start = Date.now(); const start = performance.now();
const milliseconds = 1000; const milliseconds = 1000;
await sleep(milliseconds); await sleep(milliseconds);
const end = Date.now(); const end = performance.now();
const elapsed = end - start; const elapsed = end - start;
expect(elapsed).toBeGreaterThanOrEqual(milliseconds); // Flaky test due to JS's lack of precision in setTimeout,
// so we allow for a 1ms difference
expect(elapsed).toBeGreaterThanOrEqual(milliseconds - 1);
}); });
}); });