@remotex-labs/xjet 1.2.2 → 1.3.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
@@ -6,7 +6,6 @@
6
6
  import type { xExpect } from '@remotex-labs/xjet-expect';
7
7
  import type { FunctionLikeType, FunctionType } from '@remotex-labs/xjet-expect';
8
8
  import type { FunctionType } from '@remotex-labs/xjet-expect';
9
- import type { FunctionType, RejectedValueType, ResolvedValueType } from '@remotex-labs/xjet-expect';
10
9
  import type { ContextType } from '@remotex-labs/xjet-expect';
11
10
  import type { FunctionLikeType } from '@remotex-labs/xjet-expect';
12
11
  import type { ContextType, FunctionType } from '@remotex-labs/xjet-expect';
@@ -54,8 +53,11 @@ declare global {
54
53
  const runAllTimers: typeof FakeTimer.runAllTimers;
55
54
  const useFakeTimers: typeof FakeTimer.useFakeTimers;
56
55
  const useRealTimers: typeof FakeTimer.useRealTimers;
56
+ const clearAllTimers: typeof FakeTimer.clearAllTimers;
57
+ const runAllTimersAsync: typeof FakeTimer.runAllTimersAsync;
57
58
  const advanceTimersByTime: typeof FakeTimer.advanceTimersByTime;
58
59
  const runOnlyPendingTimers: typeof FakeTimer.runOnlyPendingTimers;
60
+ const runOnlyPendingTimersAsync: typeof FakeTimer.runOnlyPendingTimersAsync;
59
61
  }
60
62
  const it: TestDirectiveInterface;
61
63
  const test: TestDirectiveInterface;
@@ -178,7 +180,7 @@ declare function fnImplementation<ReturnType, Args extends Array<unknown>, Conte
178
180
  *
179
181
  * @since 1.2.2
180
182
  */
181
- declare function mockImplementation<F extends abstract new (...args: any) => any>(method: F, implementation?: (...args: ConstructorParameters<F>) => Partial<InstanceType<F>>): MockState<(...args: ConstructorParameters<F>) => Partial<InstanceType<F>>>;
183
+ declare function mockImplementation<F extends abstract new (...args: any) => any>(method: F, implementation?: (...args: ConstructorParameters<F>) => PartialResolvedType<InstanceType<F>>): MockState<(...args: ConstructorParameters<F>) => PartialResolvedType<InstanceType<F>>>;
182
184
  /**
183
185
  * Creates a mock implementation of the provided function.
184
186
  *
@@ -213,7 +215,7 @@ declare function mockImplementation<F extends abstract new (...args: any) => any
213
215
  *
214
216
  * @since 1.2.2
215
217
  */
216
- declare function mockImplementation<F extends FunctionType>(method: F, implementation?: (...args: Parameters<F>) => Partial<ReturnType<F>>): MockState<(this: ThisParameterType<F>, ...args: Parameters<F>) => Partial<ReturnType<F>>>;
218
+ declare function mockImplementation<F extends FunctionType>(method: F, implementation?: (...args: Parameters<F>) => PartialResolvedType<ReturnType<F>>): MockState<(this: ThisParameterType<F>, ...args: Parameters<F>) => PartialResolvedType<ReturnType<F>>>;
217
219
  /**
218
220
  * Creates a mock for an element with an optional custom implementation.
219
221
  *
@@ -292,6 +294,35 @@ interface MockableFunctionInterface<F extends FunctionType> extends MockState<F>
292
294
  */
293
295
  (this: ThisParameterType<F>, ...args: Parameters<F>): ReturnType<F>;
294
296
  }
297
+ /**
298
+ * Makes properties of a type or its resolved promise value optional.
299
+ * Converts `never` to `void` to avoid unassignable types.
300
+ *
301
+ * @template T - The type to transform
302
+ *
303
+ * @remarks
304
+ * If `T` is a `PromiseLike` type, this utility unwraps it and makes the resolved value's
305
+ * properties optional.
306
+ * If `T` is `never`, it is converted to `void`.
307
+ * Otherwise, it directly makes `T`'s properties optional.
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * // Makes properties of User optional
312
+ * type MaybeUser = PartialResolvedType<User>;
313
+ *
314
+ * // Makes properties of resolved User optional
315
+ * type MaybeAsyncUser = PartialResolvedType<Promise<User>>;
316
+ *
317
+ * // Never becomes void
318
+ * type MaybeNever = PartialResolvedType<never>; // void
319
+ * ```
320
+ *
321
+ * @since 1.2.2
322
+ */
323
+ type PartialResolvedType<T> = [
324
+ T
325
+ ] extends [never] ? void : T extends PromiseLike<infer U> ? Promise<Partial<U>> : Partial<T>;
295
326
 
296
327
  /**
297
328
  * Import will remove at compile time
@@ -813,7 +844,7 @@ export declare class MockState<F extends FunctionType = FunctionType> extends Fu
813
844
  *
814
845
  * @since 1.0.0
815
846
  */
816
- mockResolvedValue(value: ResolvedValueType<ReturnType<F>>): this;
847
+ mockResolvedValue(value: PromiseValueType<ReturnType<F>>): this;
817
848
  /**
818
849
  * Adds a one-time resolved Promise return value for this mock function.
819
850
  *
@@ -847,7 +878,7 @@ export declare class MockState<F extends FunctionType = FunctionType> extends Fu
847
878
  *
848
879
  * @since 1.0.0
849
880
  */
850
- mockResolvedValueOnce(value: ResolvedValueType<ReturnType<F>>): this;
881
+ mockResolvedValueOnce(value: PromiseValueType<ReturnType<F>>): this;
851
882
  /**
852
883
  * Sets a rejected Promise return value for this mock function.
853
884
  *
@@ -884,7 +915,7 @@ export declare class MockState<F extends FunctionType = FunctionType> extends Fu
884
915
  *
885
916
  * @since 1.0.0
886
917
  */
887
- mockRejectedValue(value: RejectedValueType<ReturnType<F>>): this;
918
+ mockRejectedValue(value: PromiseValueType<ReturnType<F>>): this;
888
919
  /**
889
920
  * Adds a one-time rejected Promise return value for this mock function.
890
921
  *
@@ -920,7 +951,7 @@ export declare class MockState<F extends FunctionType = FunctionType> extends Fu
920
951
  *
921
952
  * @since 1.0.0
922
953
  */
923
- mockRejectedValueOnce(value: RejectedValueType<ReturnType<F>>): this;
954
+ mockRejectedValueOnce(value: PromiseValueType<ReturnType<F>>): this;
924
955
  /**
925
956
  * Initializes the internal state object for the mock function.
926
957
  *
@@ -1017,26 +1048,6 @@ export declare class MockState<F extends FunctionType = FunctionType> extends Fu
1017
1048
  private invokeFunction;
1018
1049
  }
1019
1050
 
1020
- /**
1021
- * Represents an interface for defining bound context and arguments.
1022
- *
1023
- * @template Context - The type of the bound `this` context, which can be an object, null, or undefined.
1024
- * @template Args - The type of the array representing bound arguments.
1025
- *
1026
- * @remarks
1027
- * This interface is designed to store binding metadata, specifically a reference to the bound `this` context
1028
- * and any arguments that are pre-applied during function binding. It is often used in scenarios involving
1029
- * dynamic contexts or partial application of functions.
1030
- *
1031
- * @see ContextType
1032
- *
1033
- * @since 1.0.0
1034
- */
1035
- interface BoundInterface<Context = unknown | null | undefined, Args = Array<unknown>> {
1036
- __boundThis?: Context;
1037
- __boundArgs?: Args;
1038
- }
1039
-
1040
1051
  /**
1041
1052
  * Import will remove at compile time
1042
1053
  */
@@ -1143,6 +1154,29 @@ interface MocksStateInterface<F extends FunctionType> {
1143
1154
  */
1144
1155
  results: Array<MockInvocationResultInterface<ReturnType<F>>>;
1145
1156
  }
1157
+ /**
1158
+ * Extracts the resolved value type from a `PromiseLike` type.
1159
+ *
1160
+ * @template T The input type to inspect.
1161
+ *
1162
+ * @remarks
1163
+ * If `T` extends `PromiseLike<infer U>`, the resulting type is `U | T`, meaning it includes both
1164
+ * the resolved value type and the original `PromiseLike` type.
1165
+ *
1166
+ * If `T` is not a `PromiseLike`, the resulting type is simply `T`.
1167
+ *
1168
+ * This utility type is useful when you want to support both synchronous and asynchronous values
1169
+ * transparently — for example, when a function may return either a raw value or a promise.
1170
+ *
1171
+ * @example
1172
+ * ```ts
1173
+ * type A = PromiseValueType<Promise<number>>; // number | Promise<number>
1174
+ * type B = PromiseValueType<string>; // string
1175
+ * ```
1176
+ *
1177
+ * @since 1.0.0
1178
+ */
1179
+ type PromiseValueType<T> = T | (T extends PromiseLike<infer U> ? U | T : never);
1146
1180
  /**
1147
1181
  * A utility type that extracts the signature of a function and allows it to be reused in an implementation.
1148
1182
  *
@@ -1173,7 +1207,27 @@ interface MocksStateInterface<F extends FunctionType> {
1173
1207
  *
1174
1208
  * @since 1.2.2
1175
1209
  */
1176
- type ImplementationType<F extends FunctionType> = FunctionLikeType<ReturnType<F>, Parameters<F>, ThisParameterType<F>>;
1210
+ type ImplementationType<F extends FunctionType> = FunctionLikeType<PromiseValueType<ReturnType<F>>, Parameters<F>, ThisParameterType<F>>;
1211
+
1212
+ /**
1213
+ * Represents an interface for defining bound context and arguments.
1214
+ *
1215
+ * @template Context - The type of the bound `this` context, which can be an object, null, or undefined.
1216
+ * @template Args - The type of the array representing bound arguments.
1217
+ *
1218
+ * @remarks
1219
+ * This interface is designed to store binding metadata, specifically a reference to the bound `this` context
1220
+ * and any arguments that are pre-applied during function binding. It is often used in scenarios involving
1221
+ * dynamic contexts or partial application of functions.
1222
+ *
1223
+ * @see ContextType
1224
+ *
1225
+ * @since 1.0.0
1226
+ */
1227
+ interface BoundInterface<Context = unknown | null | undefined, Args = Array<unknown>> {
1228
+ __boundThis?: Context;
1229
+ __boundArgs?: Args;
1230
+ }
1177
1231
 
1178
1232
  /**
1179
1233
  * A base error class that extends the standard Error class with enhanced JSON serialization and location tracking
@@ -1299,7 +1353,7 @@ declare class ExecutionError extends Error {
1299
1353
  * @see DeepSearchInterface
1300
1354
  * @since 1.0.0
1301
1355
  */
1302
- declare function deepSearchObject(target: Record<string | symbol, unknown>, element: unknown, key?: string, maxDepth?: number): DeepSearchInterface | null;
1356
+ declare function deepSearchObject(target: Record<string | symbol, unknown>, element: unknown, key?: string | symbol, maxDepth?: number): DeepSearchInterface | null;
1303
1357
  /**
1304
1358
  * Resolves property references that may be affected by ESBuild's `__toESM` transformation.
1305
1359
  *
@@ -1560,7 +1614,7 @@ declare function spyOnProxyGet<T extends Record<string | symbol, unknown>, K ext
1560
1614
  *
1561
1615
  * @since 1.0.0
1562
1616
  */
1563
- declare function spyOnImplementation<T extends object, K extends keyof T>(target: T, key: K): T[K] extends FunctionType ? MockState<(this: ThisParameterType<T[K]>, ...args: Parameters<T[K]>) => ReturnType<T[K]>> : MockState<() => T[K]>;
1617
+ declare function spyOnImplementation<T extends object, K extends keyof T>(target: T, key: K): T[K] extends FunctionType ? MockState<(this: ThisParameterType<T[K]>, ...args: Parameters<T[K]>) => PartialResolvedType<ReturnType<T[K]>>> : MockState<() => T[K]>;
1564
1618
 
1565
1619
  /**
1566
1620
  * Interface representing the internal state of a mock proxy.
@@ -1720,6 +1774,26 @@ declare class TimerService {
1720
1774
  * @since 1.1.0
1721
1775
  */
1722
1776
  useRealTimers(): void;
1777
+ /**
1778
+ * Clears all active fake timers.
1779
+ *
1780
+ * @remarks
1781
+ * This method removes every timer currently stored in {@link timers},
1782
+ * effectively resetting the fake timer state without advancing time.
1783
+ * It is useful for cleaning up between tests to ensure no lingering
1784
+ * scheduled callbacks remain.
1785
+ *
1786
+ * @example
1787
+ * ```ts
1788
+ * useFakeTimers();
1789
+ * setTimeout(() => console.log('A'), 100);
1790
+ * clearAllTimers(); // removes all scheduled timers
1791
+ * advanceTimersByTime(100); // nothing happens
1792
+ * ```
1793
+ *
1794
+ * @since 1.3.0
1795
+ */
1796
+ clearAllTimers(): void;
1723
1797
  /**
1724
1798
  * Advances the simulated clock by a specific number of milliseconds and
1725
1799
  * executes all timers whose scheduled time has elapsed.
@@ -1790,6 +1864,51 @@ declare class TimerService {
1790
1864
  * @since 1.1.0
1791
1865
  */
1792
1866
  runOnlyPendingTimers(): void;
1867
+ /**
1868
+ * Asynchronous equivalent of {@link runAllTimers}.
1869
+ *
1870
+ * @remarks
1871
+ * This method first yields to the event loop to allow any pending promises
1872
+ * to resolve before executing all remaining fake timers.
1873
+ * It ensures a deterministic sequence when timers and microtasks coexist.
1874
+ *
1875
+ * @example
1876
+ * ```ts
1877
+ * useFakeTimers();
1878
+ * Promise.resolve().then(() => console.log('microtask'));
1879
+ * setTimeout(() => console.log('timer'), 0);
1880
+ * await timerService.runAllTimersAsync();
1881
+ * // Logs:
1882
+ * // microtask
1883
+ * // timer
1884
+ * ```
1885
+ *
1886
+ * @since 1.3.0
1887
+ */
1888
+ runAllTimersAsync(): Promise<void>;
1889
+ /**
1890
+ * Asynchronous equivalent of {@link runOnlyPendingTimers}.
1891
+ *
1892
+ * @remarks
1893
+ * This method first yields to the event loop to allow any pending promises
1894
+ * to resolve before executing only currently pending fake timers.
1895
+ * Timers scheduled during execution will not run until explicitly advanced later.
1896
+ *
1897
+ * @example
1898
+ * ```ts
1899
+ * useFakeTimers();
1900
+ * setTimeout(() => {
1901
+ * console.log('first');
1902
+ * setTimeout(() => console.log('second'), 100);
1903
+ * }, 100);
1904
+ * await timerService.runOnlyPendingTimersAsync();
1905
+ * // Logs:
1906
+ * // first
1907
+ * ```
1908
+ *
1909
+ * @since 1.3.0
1910
+ */
1911
+ runOnlyPendingTimersAsync(): Promise<void>;
1793
1912
  }
1794
1913
  /**
1795
1914
  * Globally enables fake timers using the shared {@link TimerService}.
@@ -1853,6 +1972,31 @@ declare function useRealTimers(): void;
1853
1972
  * @since 1.1.0
1854
1973
  */
1855
1974
  declare function runAllTimers(): void;
1975
+ /**
1976
+ * Removes all scheduled fake timers from the {@link TimerService}.
1977
+ *
1978
+ * @remarks
1979
+ * This function clears all active timers registered in the shared timer service,
1980
+ * effectively canceling any pending callbacks that would have run during
1981
+ * timer advancement.
1982
+ *
1983
+ * It's useful for resetting the fake timer state between test cases to ensure
1984
+ * no lingering timers affect further tests or for scenarios where you
1985
+ * need to abort all pending operations.
1986
+ *
1987
+ * @example
1988
+ * ```ts
1989
+ * useFakeTimers();
1990
+ * setTimeout(() => console.log('A'), 100);
1991
+ * setTimeout(() => console.log('B'), 200);
1992
+ *
1993
+ * clearAllTimers(); // removes all scheduled timers
1994
+ * advanceTimersByTime(1000); // nothing happens, not show any logs
1995
+ * ```
1996
+ *
1997
+ * @since 1.3.0
1998
+ */
1999
+ declare function clearAllTimers(): void;
1856
2000
  /**
1857
2001
  * Executes only the timers that are pending at the time of invocation.
1858
2002
  *
@@ -1900,6 +2044,48 @@ declare function runOnlyPendingTimers(): void;
1900
2044
  * @since 1.1.0
1901
2045
  */
1902
2046
  declare function advanceTimersByTime(ms?: number): void;
2047
+ /**
2048
+ * Asynchronous equivalent of {@link runAllTimers}.
2049
+ *
2050
+ * @remarks
2051
+ * Yields to the event loop before running all pending fake timers.
2052
+ * Useful when working with both Promises and fake timers.
2053
+ *
2054
+ * @example
2055
+ * ```ts
2056
+ * xJet.useFakeTimers();
2057
+ * Promise.resolve().then(() => console.log('promise done'));
2058
+ * setTimeout(() => console.log('timeout done'), 0);
2059
+ * await xJet.runAllTimersAsync();
2060
+ * // Logs:
2061
+ * // promise done
2062
+ * // timeout done
2063
+ * ```
2064
+ *
2065
+ * @since 1.3.0
2066
+ */
2067
+ declare function runAllTimersAsync(): Promise<void>;
2068
+ /**
2069
+ * Asynchronous equivalent of {@link runOnlyPendingTimers}.
2070
+ *
2071
+ * @remarks
2072
+ * Yields to the event loop before running only timers that are currently pending.
2073
+ * Any timers scheduled by those callbacks will not be executed until a later call.
2074
+ *
2075
+ * @example
2076
+ * ```ts
2077
+ * useFakeTimers();
2078
+ * setTimeout(() => {
2079
+ * console.log('first');
2080
+ * setTimeout(() => console.log('second'), 100);
2081
+ * }, 100);
2082
+ * await runOnlyPendingTimersAsync();
2083
+ * // Logs only "first"
2084
+ * ```
2085
+ *
2086
+ * @since 1.3.0
2087
+ */
2088
+ declare function runOnlyPendingTimersAsync(): Promise<void>;
1903
2089
 
1904
2090
  /**
1905
2091
  * Interface representing a single timer stored and executed by the {@link TimerService}.