@remotex-labs/xjet 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -33,17 +33,31 @@ import type { PromiseRejectType, PromiseResolveType } from '@remotex-labs/xjet-e
33
33
  */
34
34
  declare global {
35
35
  namespace xJet {
36
+ /**
37
+ * Mock
38
+ */
36
39
  const fn: typeof fnImplementation;
37
40
  const mock: typeof mockImplementation;
38
41
  const spyOn: typeof spyOnImplementation;
42
+ const clearAllMocks: () => void;
43
+ const resetAllMocks: () => void;
44
+ const restoreAllMocks: () => void;
45
+ /**
46
+ * Logs
47
+ */
39
48
  const log: typeof console.log;
40
49
  const info: typeof console.info;
41
50
  const warn: typeof console.warn;
42
51
  const error: typeof console.error;
43
52
  const debug: typeof console.error;
44
- const clearAllMocks: () => void;
45
- const resetAllMocks: () => void;
46
- const restoreAllMocks: () => void;
53
+ /**
54
+ * Timers
55
+ */
56
+ const runAllTimers: typeof FakeTimer.runAllTimers;
57
+ const useFakeTimers: typeof FakeTimer.useFakeTimers;
58
+ const useRealTimers: typeof FakeTimer.useRealTimers;
59
+ const advanceTimersByTime: typeof FakeTimer.advanceTimersByTime;
60
+ const runOnlyPendingTimers: typeof FakeTimer.runOnlyPendingTimers;
47
61
  }
48
62
  const it: TestDirectiveInterface;
49
63
  const test: TestDirectiveInterface;
@@ -210,7 +224,6 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
210
224
  /**
211
225
  * Flag to detect mock functions
212
226
  */
213
- readonly isMock: boolean;
214
227
  readonly xJetMock: boolean;
215
228
  /**
216
229
  * The `state` property holds the detailed state of the mock invocations.
@@ -1192,6 +1205,430 @@ type ConstructorKeysType<T> = RemoveIndexType<keyof PropertiesWithConstructorsTy
1192
1205
  */
1193
1206
  type KeysExtendingConstructorType<T> = T extends ConstructorType ? keyof RemoveIndexType<T> : never;
1194
1207
 
1208
+ /**
1209
+ * Import will remove at compile time
1210
+ */
1211
+ /**
1212
+ * Provides a virtual timer system that mimics native `setTimeout`/`setInterval`
1213
+ * while allowing controlled execution for deterministic tests.
1214
+ *
1215
+ * @remarks
1216
+ * This service replaces the global timing functions with fake timers so that
1217
+ * time can be manually advanced and callbacks triggered predictably.
1218
+ * It is intended for unit testing scenarios where you need full control over
1219
+ * asynchronous timing without waiting in real time.
1220
+ *
1221
+ * @example
1222
+ * ```ts
1223
+ * useFakeTimers();
1224
+ * setTimeout(() => console.log('done'), 1000);
1225
+ * advanceTimersByTime(1000); // logs 'done' immediately
1226
+ * useRealTimers();
1227
+ * ```
1228
+ *
1229
+ * @since 1.1.0
1230
+ */
1231
+ declare class TimerService {
1232
+ /**
1233
+ * Active timers managed by the fake timer engine.
1234
+ * @since 1.1.0
1235
+ */
1236
+ readonly timers: Map<number, TimerInterface>;
1237
+ /**
1238
+ * Stores original `Date.now` to restore when real timers are re-enabled.
1239
+ * @since 1.1.0
1240
+ */
1241
+ readonly originalDateNow: () => number;
1242
+ /**
1243
+ * Stores original global `setTimeout`.
1244
+ * @since 1.1.0
1245
+ */
1246
+ readonly originalSetTimeout: typeof setTimeout;
1247
+ /**
1248
+ * Stores original global `setInterval`.
1249
+ * @since 1.1.0
1250
+ */
1251
+ readonly originalSetInterval: typeof setInterval;
1252
+ /**
1253
+ * Stores original global `clearTimeout`.
1254
+ * @since 1.1.0
1255
+ */
1256
+ readonly originalClearTimeout: typeof clearTimeout;
1257
+ /**
1258
+ * Stores original global `clearInterval`.
1259
+ * @since 1.1.0
1260
+ */
1261
+ readonly originalClearInterval: typeof clearInterval;
1262
+ /**
1263
+ * Simulated current timestamp for the fake timers.
1264
+ * @since 1.1.0
1265
+ */
1266
+ private now;
1267
+ /**
1268
+ * Incremental id used to register timers uniquely.
1269
+ * @since 1.1.0
1270
+ */
1271
+ private nextId;
1272
+ /**
1273
+ * Replaces the global timer functions with in-memory fakes.
1274
+ *
1275
+ * @remarks
1276
+ * After calling this method, any calls to `setTimeout`, `setInterval`,
1277
+ * `clearTimeout`, or `clearInterval` will be intercepted and stored in the
1278
+ * {@link timers} map instead of scheduling real OS timers.
1279
+ * This allows tests to control time progression manually using
1280
+ * {@link advanceTimersByTime}, {@link runAllTimers}, or
1281
+ * {@link runOnlyPendingTimers}.
1282
+ *
1283
+ * @example
1284
+ * ```ts
1285
+ * timerService.useFakeTimers();
1286
+ * const id = setTimeout(() => console.log('done'), 1000);
1287
+ * timerService.advanceTimersByTime(1000); // logs "done" immediately
1288
+ * ```
1289
+ *
1290
+ * @since 1.1.0
1291
+ */
1292
+ useFakeTimers(): void;
1293
+ /**
1294
+ * Restores the original global timer APIs and `Date.now`.
1295
+ *
1296
+ * @remarks
1297
+ * This method undoes the effects of {@link useFakeTimers}, re-binding
1298
+ * the native implementations of `setTimeout`, `setInterval`,
1299
+ * `clearTimeout`, `clearInterval`, and `Date.now`.
1300
+ * After calling this, timers once again behave according to real system
1301
+ * time and manual advancement methods such as
1302
+ * {@link advanceTimersByTime} no longer apply.
1303
+ *
1304
+ * @example
1305
+ * ```ts
1306
+ * timerService.useFakeTimers();
1307
+ * // ...run tests with controlled time...
1308
+ * timerService.useRealTimers(); // restore native timers
1309
+ * ```
1310
+ *
1311
+ * @since 1.1.0
1312
+ */
1313
+ useRealTimers(): void;
1314
+ /**
1315
+ * Advances the simulated clock by a specific number of milliseconds and
1316
+ * executes all timers whose scheduled time has elapsed.
1317
+ *
1318
+ * @remarks
1319
+ * Use this method after calling {@link useFakeTimers} to fast-forward the
1320
+ * internal clock without waiting in real time.
1321
+ * Any `setTimeout` or `setInterval` callbacks scheduled within the
1322
+ * advanced period will run immediately in order of their scheduled time.
1323
+ *
1324
+ * @param ms - The number of milliseconds to move the simulated time forward.
1325
+ *
1326
+ * @example
1327
+ * ```ts
1328
+ * timerService.useFakeTimers();
1329
+ * setTimeout(() => console.log('done'), 500);
1330
+ * timerService.advanceTimersByTime(500); // logs "done"
1331
+ * ```
1332
+ *
1333
+ * @since 1.1.0
1334
+ */
1335
+ advanceTimersByTime(ms: number): void;
1336
+ /**
1337
+ * Executes every scheduled timer until none remain.
1338
+ *
1339
+ * @remarks
1340
+ * This method repeatedly advances the simulated clock to the next
1341
+ * scheduled timer and runs its callback until the {@link timers} map
1342
+ * is empty.
1343
+ * It is useful when you want to immediately flush **all** pending
1344
+ * `setTimeout` or `setInterval` callbacks regardless of their delay,
1345
+ * without specifying a time increment.
1346
+ *
1347
+ * @example
1348
+ * ```ts
1349
+ * timerService.useFakeTimers();
1350
+ * setTimeout(() => console.log('A'), 100);
1351
+ * setTimeout(() => console.log('B'), 200);
1352
+ * timerService.runAllTimers(); // logs "A" then "B"
1353
+ * ```
1354
+ *
1355
+ * @since 1.1.0
1356
+ */
1357
+ runAllTimers(): void;
1358
+ /**
1359
+ * Executes only the timers that are currently pending at the time of call,
1360
+ * without running any new timers that may be scheduled by those callbacks.
1361
+ *
1362
+ * @remarks
1363
+ * Unlike {@link runAllTimers}, this method captures the set of timers
1364
+ * that exist when the method is invoked and restricts execution to that
1365
+ * initial set.
1366
+ * If any of those timers schedule additional timers while running,
1367
+ * the newly scheduled ones will **not** be executed during this call.
1368
+ *
1369
+ * @example
1370
+ * ```ts
1371
+ * timerService.useFakeTimers();
1372
+ * setTimeout(() => {
1373
+ * console.log('first');
1374
+ * setTimeout(() => console.log('second'), 100);
1375
+ * }, 100);
1376
+ *
1377
+ * timerService.runOnlyPendingTimers();
1378
+ * // Logs only "first" because "second" was created afterward.
1379
+ * ```
1380
+ *
1381
+ * @since 1.1.0
1382
+ */
1383
+ runOnlyPendingTimers(): void;
1384
+ }
1385
+ /**
1386
+ * Globally enables fake timers using the shared {@link TimerService}.
1387
+ *
1388
+ * @remarks
1389
+ * After calling this function, all calls to `setTimeout`, `setInterval`,
1390
+ * `clearTimeout`, and `clearInterval` will be intercepted by the
1391
+ * {@link TimerService} and stored in-memory instead of executing in real time.
1392
+ * This allows deterministic testing by manually advancing time with
1393
+ * {@link advanceTimersByTime}, {@link runAllTimers}, or
1394
+ * {@link runOnlyPendingTimers}.
1395
+ *
1396
+ * @example
1397
+ * ```ts
1398
+ * useFakeTimers();
1399
+ * setTimeout(() => console.log('done'), 1000);
1400
+ * advanceTimersByTime(1000); // logs "done" immediately
1401
+ * ```
1402
+ *
1403
+ * @since 1.1.0
1404
+ */
1405
+ declare function useFakeTimers(): void;
1406
+ /**
1407
+ * Restores real timers globally using the shared {@link TimerService}.
1408
+ *
1409
+ * @remarks
1410
+ * This function undoes the effects of {@link useFakeTimers}, restoring the
1411
+ * native implementations of `setTimeout`, `setInterval`, `clearTimeout`,
1412
+ * `clearInterval`, and `Date.now`.
1413
+ * After calling this, timers once again behave according to real system time,
1414
+ * and manual advancement methods like {@link advanceTimersByTime} no longer apply.
1415
+ *
1416
+ * @example
1417
+ * ```ts
1418
+ * useFakeTimers();
1419
+ * // ...run tests with controlled time...
1420
+ * useRealTimers(); // restore native timers
1421
+ * ```
1422
+ *
1423
+ * @since 1.1.0
1424
+ */
1425
+ declare function useRealTimers(): void;
1426
+ /**
1427
+ * Executes all timers currently registered in the {@link TimerService}.
1428
+ *
1429
+ * @remarks
1430
+ * This function repeatedly runs all pending `setTimeout` and `setInterval`
1431
+ * callbacks until no timers remain.
1432
+ * It is equivalent to calling {@link TimerService.runAllTimers} on the
1433
+ * injected service instance and is useful for immediately flushing
1434
+ * all scheduled timers in tests.
1435
+ *
1436
+ * @example
1437
+ * ```ts
1438
+ * useFakeTimers();
1439
+ * setTimeout(() => console.log('A'), 100);
1440
+ * setTimeout(() => console.log('B'), 200);
1441
+ * runAllTimers(); // logs "A" then "B"
1442
+ * ```
1443
+ *
1444
+ * @since 1.1.0
1445
+ */
1446
+ declare function runAllTimers(): void;
1447
+ /**
1448
+ * Executes only the timers that are pending at the time of invocation.
1449
+ *
1450
+ * @remarks
1451
+ * This function runs the callbacks of timers that exist when the function
1452
+ * is called, without executing any new timers that may be scheduled
1453
+ * during their execution.
1454
+ * It delegates to {@link TimerService.runOnlyPendingTimers} on the injected
1455
+ * service instance.
1456
+ *
1457
+ * @example
1458
+ * ```ts
1459
+ * useFakeTimers();
1460
+ * setTimeout(() => {
1461
+ * console.log('first');
1462
+ * setTimeout(() => console.log('second'), 100);
1463
+ * }, 100);
1464
+ *
1465
+ * runOnlyPendingTimers();
1466
+ * // Logs only "first"; "second" is not executed yet
1467
+ * ```
1468
+ *
1469
+ * @since 1.1.0
1470
+ */
1471
+ declare function runOnlyPendingTimers(): void;
1472
+ /**
1473
+ * Advances the simulated time by a specified number of milliseconds and
1474
+ * executes all timers that are due.
1475
+ *
1476
+ * @remarks
1477
+ * This function delegates to {@link TimerService.advanceTimersByTime}
1478
+ * on the injected service instance.
1479
+ * It is intended to be used after {@link useFakeTimers} to fast-forward
1480
+ * time in tests without waiting for real timers.
1481
+ *
1482
+ * @param ms - The number of milliseconds to advance (default is `0`).
1483
+ *
1484
+ * @example
1485
+ * ```ts
1486
+ * useFakeTimers();
1487
+ * setTimeout(() => console.log('done'), 500);
1488
+ * advanceTimersByTime(500); // logs "done"
1489
+ * ```
1490
+ *
1491
+ * @since 1.1.0
1492
+ */
1493
+ declare function advanceTimersByTime(ms?: number): void;
1494
+
1495
+ /**
1496
+ * Interface representing a single timer stored and executed by the {@link TimerService}.
1497
+ *
1498
+ * @remarks
1499
+ * This interface tracks all properties needed to manage both one-time
1500
+ * and repeating timers within the fake timer system.
1501
+ *
1502
+ * @example
1503
+ * ```ts
1504
+ * const timer: TimerInterface = {
1505
+ * id: 1,
1506
+ * time: 1000,
1507
+ * args: [],
1508
+ * callback: () => console.log('done'),
1509
+ * interval: null,
1510
+ * };
1511
+ * ```
1512
+ *
1513
+ * @since 1.1.0
1514
+ */
1515
+ interface TimerInterface {
1516
+ /**
1517
+ * Unique identifier for the timer.
1518
+ * @since 1.1.0
1519
+ */
1520
+ id: number;
1521
+ /**
1522
+ * The simulated timestamp (in milliseconds) when the timer is next due to run.
1523
+ * @since 1.1.0
1524
+ */
1525
+ time: number;
1526
+ /**
1527
+ * Arguments passed to the timer's callback function.
1528
+ * @since 1.1.0
1529
+ */
1530
+ args: Array<unknown>;
1531
+ /**
1532
+ * Callback function to execute when the timer triggers.
1533
+ * @since 1.1.0
1534
+ */
1535
+ callback: () => void;
1536
+ /**
1537
+ * Interval in milliseconds for repeating timers.
1538
+ * `null` indicates a one-time timer (like `setTimeout`), otherwise behaves like `setInterval`.
1539
+ *
1540
+ * @since 1.1.0
1541
+ */
1542
+ interval: number | null;
1543
+ }
1544
+
1545
+ /**
1546
+ * Import will remove at compile time
1547
+ */
1548
+ /**
1549
+ * Marks a class as injectable and stores its configuration metadata.
1550
+ *
1551
+ * @template T - The type of the class instance
1552
+ * @template Args - The types of arguments accepted by the constructor, defaults to `unknown[]`
1553
+ *
1554
+ * @param options - Optional configuration for the injectable, including scope and factory
1555
+ *
1556
+ * @example
1557
+ * ```ts
1558
+ * @Injectable({ scope: 'singleton' })
1559
+ * class MyService {}
1560
+ * ```
1561
+ *
1562
+ * @see InjectableOptionsInterface
1563
+ * @since 1.0.0
1564
+ */
1565
+ declare function Injectable<T = unknown, Args extends Array<unknown> = unknown[]>(options?: InjectableOptionsInterface): (target: new (...args: Args) => T) => void;
1566
+ /**
1567
+ * Resolves and returns an instance of the given injectable token.
1568
+ *
1569
+ * @template T - The type of the instance to return
1570
+ * @template Args - The types of arguments passed to the constructor or factory
1571
+ *
1572
+ * @param token - The injectable class or token to resolve
1573
+ * @param args - Arguments to pass to the constructor or factory
1574
+ *
1575
+ * @returns The resolved instance of type `T`
1576
+ *
1577
+ * @throws Error - If the token is not registered as injectable via `@Injectable`
1578
+ *
1579
+ * @remarks
1580
+ * If the injectable is marked with scope `'singleton'`, the same instance will be returned
1581
+ * on the following calls. Otherwise, a new instance is created for each call.
1582
+ *
1583
+ * @example
1584
+ * ```ts
1585
+ * const service = inject(MyService);
1586
+ * ```
1587
+ *
1588
+ * @see TokenElementType
1589
+ * @since 1.0.0
1590
+ */
1591
+ declare function inject<T, Args extends Array<unknown>>(token: TokenElementType<T, Args>, ...args: Args): T;
1592
+
1593
+ /**
1594
+ * Import will remove at compile time
1595
+ */
1596
+ /**
1597
+ * Represents a constructor type for a given element.
1598
+ *
1599
+ * @template T - The type of the instance created by the constructor
1600
+ * @template Args - The types of arguments accepted by the constructor, defaults to `unknown[]`
1601
+ *
1602
+ * @see FunctionType
1603
+ * @since 1.0.0
1604
+ */
1605
+ type TokenElementType<T, Args extends Array<unknown> = unknown[]> = new (...args: Args) => T;
1606
+ /**
1607
+ * Options for configuring an injectable service.
1608
+ *
1609
+ * @remarks
1610
+ * Used by the {@link Injectable} decorator to specify lifecycle scope
1611
+ * and custom factory function for service instantiation.
1612
+ *
1613
+ * @since 1.0.0
1614
+ */
1615
+ interface InjectableOptionsInterface {
1616
+ /**
1617
+ * Lifecycle scope of the service.
1618
+ * - `'singleton'` - Only one instance is created and shared
1619
+ * - `'transient'` - A new instance is created each time it is requested
1620
+ *
1621
+ * @default 'singleton'
1622
+ * @since 1.0.0
1623
+ */
1624
+ scope?: 'singleton' | 'transient';
1625
+ /**
1626
+ * Custom factory function used to create the service instance.
1627
+ * @since 1.0.0
1628
+ */
1629
+ factory?: FunctionType;
1630
+ }
1631
+
1195
1632
  /**
1196
1633
  * Import will remove at compile time
1197
1634
  */
@@ -2102,93 +2539,6 @@ interface ContextInterface {
2102
2539
  afterAllErrors: Array<unknown>;
2103
2540
  }
2104
2541
 
2105
- /**
2106
- * Import will remove at compile time
2107
- */
2108
- /**
2109
- * Marks a class as injectable and stores its configuration metadata.
2110
- *
2111
- * @template T - The type of the class instance
2112
- * @template Args - The types of arguments accepted by the constructor, defaults to `unknown[]`
2113
- *
2114
- * @param options - Optional configuration for the injectable, including scope and factory
2115
- *
2116
- * @example
2117
- * ```ts
2118
- * @Injectable({ scope: 'singleton' })
2119
- * class MyService {}
2120
- * ```
2121
- *
2122
- * @see InjectableOptionsInterface
2123
- * @since 1.0.0
2124
- */
2125
- declare function Injectable<T = unknown, Args extends Array<unknown> = unknown[]>(options?: InjectableOptionsInterface): (target: new (...args: Args) => T) => void;
2126
- /**
2127
- * Resolves and returns an instance of the given injectable token.
2128
- *
2129
- * @template T - The type of the instance to return
2130
- * @template Args - The types of arguments passed to the constructor or factory
2131
- *
2132
- * @param token - The injectable class or token to resolve
2133
- * @param args - Arguments to pass to the constructor or factory
2134
- *
2135
- * @returns The resolved instance of type `T`
2136
- *
2137
- * @throws Error - If the token is not registered as injectable via `@Injectable`
2138
- *
2139
- * @remarks
2140
- * If the injectable is marked with scope `'singleton'`, the same instance will be returned
2141
- * on the following calls. Otherwise, a new instance is created for each call.
2142
- *
2143
- * @example
2144
- * ```ts
2145
- * const service = inject(MyService);
2146
- * ```
2147
- *
2148
- * @see TokenElementType
2149
- * @since 1.0.0
2150
- */
2151
- declare function inject<T, Args extends Array<unknown>>(token: TokenElementType<T, Args>, ...args: Args): T;
2152
-
2153
- /**
2154
- * Import will remove at compile time
2155
- */
2156
- /**
2157
- * Represents a constructor type for a given element.
2158
- *
2159
- * @template T - The type of the instance created by the constructor
2160
- * @template Args - The types of arguments accepted by the constructor, defaults to `unknown[]`
2161
- *
2162
- * @see FunctionType
2163
- * @since 1.0.0
2164
- */
2165
- type TokenElementType<T, Args extends Array<unknown> = unknown[]> = new (...args: Args) => T;
2166
- /**
2167
- * Options for configuring an injectable service.
2168
- *
2169
- * @remarks
2170
- * Used by the {@link Injectable} decorator to specify lifecycle scope
2171
- * and custom factory function for service instantiation.
2172
- *
2173
- * @since 1.0.0
2174
- */
2175
- interface InjectableOptionsInterface {
2176
- /**
2177
- * Lifecycle scope of the service.
2178
- * - `'singleton'` - Only one instance is created and shared
2179
- * - `'transient'` - A new instance is created each time it is requested
2180
- *
2181
- * @default 'singleton'
2182
- * @since 1.0.0
2183
- */
2184
- scope?: 'singleton' | 'transient';
2185
- /**
2186
- * Custom factory function used to create the service instance.
2187
- * @since 1.0.0
2188
- */
2189
- factory?: FunctionType;
2190
- }
2191
-
2192
2542
  /**
2193
2543
  * Imports
2194
2544
  */