From: Jérôme Benoit Date: Fri, 20 Feb 2026 19:08:29 +0000 (+0100) Subject: fix(tests): use fake timers for sleep X-Git-Tag: ocpp-server@v2.4.0~18 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=43042ee0ae13ec462cf181e93733f05d9368f647;p=e-mobility-charging-stations-simulator.git fix(tests): use fake timers for sleep Closes #1684 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus --- diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 43d34c39..ba9fdb2b 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -60,17 +60,19 @@ await describe('Utils test suite', async () => { expect(validateUUID(true)).toBe(false) }) - await it('Verify sleep()', async () => { - const start = performance.now() - const delay = 10 - const timeout = await sleep(delay) - const stop = performance.now() - const actualDelay = stop - start - expect(timeout).toBeDefined() - expect(typeof timeout).toBe('object') - expect(actualDelay).toBeGreaterThanOrEqual(delay - 0.6) // Allow 0.6ms tolerance - expect(actualDelay).toBeLessThan(delay + 50) // Allow 50ms tolerance - clearTimeout(timeout) + await it('Verify sleep()', async t => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + try { + const delay = 10 + const sleepPromise = sleep(delay) + t.mock.timers.tick(delay) + const timeout = await sleepPromise + expect(timeout).toBeDefined() + expect(typeof timeout).toBe('object') + clearTimeout(timeout) + } finally { + t.mock.timers.reset() + } }) await it('Verify formatDurationMilliSeconds()', () => { diff --git a/tests/worker/WorkerUtils.test.ts b/tests/worker/WorkerUtils.test.ts index d6c9747c..b0297914 100644 --- a/tests/worker/WorkerUtils.test.ts +++ b/tests/worker/WorkerUtils.test.ts @@ -29,24 +29,23 @@ await describe('WorkerUtils test suite', async () => { }).toThrow(SyntaxError) }) - await it('Verify sleep()', async () => { - const startTime = performance.now() - const delay = 10 // 10ms for fast test execution - - const timeout = await sleep(delay) - const endTime = performance.now() - const actualDelay = endTime - startTime - - // Verify timeout object is returned - expect(timeout).toBeDefined() - expect(typeof timeout).toBe('object') - - // Verify actual delay is approximately correct (within reasonable tolerance) - expect(actualDelay).toBeGreaterThanOrEqual(delay - 0.6) // Allow 0.6ms tolerance - expect(actualDelay).toBeLessThan(delay + 50) // Allow 50ms tolerance - - // Clean up timeout - clearTimeout(timeout) + await it('Verify sleep()', async t => { + t.mock.timers.enable({ apis: ['setTimeout'] }) + try { + const delay = 10 // 10ms for fast test execution + const sleepPromise = sleep(delay) + t.mock.timers.tick(delay) + const timeout = await sleepPromise + + // Verify timeout object is returned + expect(timeout).toBeDefined() + expect(typeof timeout).toBe('object') + + // Clean up timeout + clearTimeout(timeout) + } finally { + t.mock.timers.reset() + } }) await it('Verify defaultExitHandler()', t => {