]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
docs(tests): document timer mock pattern for test reuse
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 Feb 2026 16:12:21 +0000 (17:12 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 Feb 2026 16:12:21 +0000 (17:12 +0100)
tests/utils/Utils.test.ts
ui/web/tests/unit/CSTable.test.ts [moved from ui/web/tests/unit/CSTable.spec.ts with 100% similarity]

index 3557757b300342d29fcfd627c291d41a942d4ab6..1abbd5bdc24ebeb628fdfcf66aa36b30782430c6 100644 (file)
@@ -78,6 +78,35 @@ await describe('Utils test suite', async () => {
   })
 
   await it('Verify sleep()', async t => {
+    /**
+     * Timer mock pattern for testing asynchronous timer-based operations.
+     * Uses Node.js test module's built-in timer mocking API.
+     * @example
+     * // Enable timer mocking with setTimeout API
+     * t.mock.timers.enable({ apis: ['setTimeout'] })
+     *
+     * try {
+     *   const delay = 10
+     *   const sleepPromise = sleep(delay)
+     *   // Advance mocked timers by specified milliseconds
+     *   t.mock.timers.tick(delay)
+     *   const timeout = await sleepPromise
+     *   expect(timeout).toBeDefined()
+     * } finally {
+     *   // Always reset timers after test to prevent side effects
+     *   t.mock.timers.reset()
+     * }
+     * @description
+     * This pattern demonstrates:
+     * - `t.mock.timers.enable()`: Activates timer mocking for specified APIs (e.g., setTimeout)
+     * - `t.mock.timers.tick()`: Advances the mocked internal clock by the given milliseconds
+     * - `t.mock.timers.reset()`: Clears mocked timers; use in finally block to ensure cleanup
+     *
+     * Useful for testing:
+     * - Timeout and interval-based logic
+     * - Async functions that depend on timing
+     * - Avoiding slow tests caused by actual delays
+     */
     t.mock.timers.enable({ apis: ['setTimeout'] })
     try {
       const delay = 10