]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix(tests): use fake timers for sleep
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 20 Feb 2026 19:08:29 +0000 (20:08 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 20 Feb 2026 19:09:10 +0000 (20:09 +0100)
Closes #1684

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
tests/utils/Utils.test.ts
tests/worker/WorkerUtils.test.ts

index 43d34c394a6a65783e928231515381151c60af0d..ba9fdb2b7ca03214f15d326306bea2e7dd8f5dc6 100644 (file)
@@ -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()', () => {
index d6c9747c9accc1a0d408e5ee4f0efc8db04f9fc5..b0297914bfd2e21aabbae0a66e74ac1d02f76cc1 100644 (file)
@@ -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 => {