@remotex-labs/xjet 1.1.1 → 1.2.1
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/README.md +1 -0
- package/dist/bash.js +1 -1
- package/dist/bash.js.map +1 -1
- package/dist/index.d.ts +275 -74
- package/dist/index.js +21 -21
- package/dist/index.js.map +3 -3
- package/dist/shared.d.ts +354 -153
- package/dist/shared.js +35 -35
- package/dist/shared.js.map +5 -5
- package/package.json +9 -9
package/dist/shared.d.ts
CHANGED
|
@@ -65,16 +65,25 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
65
65
|
*/
|
|
66
66
|
private state;
|
|
67
67
|
/**
|
|
68
|
-
* A private function that restores the
|
|
69
|
-
* It resets the mock to its initial state, typically used when restoring
|
|
70
|
-
* the mock after a call to `mockReset` or `mockRestore`.
|
|
68
|
+
* A private function that restores the mock's original implementation.
|
|
71
69
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* The `restore` function is responsible for resetting the mock to its initial state.
|
|
72
|
+
* It works in conjunction with methods like `mockReset` and `mockRestore` to ensure
|
|
73
|
+
* proper restoration of the mock's behavior.
|
|
74
74
|
*
|
|
75
|
-
*
|
|
75
|
+
* Responsibilities:
|
|
76
|
+
* - Returning the original implementation when restoring the mock
|
|
77
|
+
* - Ensuring consistent reset behavior across mock operations
|
|
78
|
+
* - Supporting the {@link mockRestore} method's functionality
|
|
79
|
+
* - Maintaining mock state integrity during restoration
|
|
80
|
+
*
|
|
81
|
+
* @see MockState.mockRestore
|
|
82
|
+
* @see MockState.originalImplementation
|
|
83
|
+
*
|
|
84
|
+
* @since 1.2.0
|
|
76
85
|
*/
|
|
77
|
-
private readonly restore
|
|
86
|
+
private readonly restore?;
|
|
78
87
|
/**
|
|
79
88
|
* A private array that stores the implementations queued to be executed
|
|
80
89
|
* for future invocations of the mock.
|
|
@@ -89,7 +98,7 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
89
98
|
private queuedImplementations;
|
|
90
99
|
/**
|
|
91
100
|
* A private property that holds the current implementation of the mock function.
|
|
92
|
-
* This function is executed whenever the mock is invoked
|
|
101
|
+
* This function is executed whenever the mock is invoked and can be changed
|
|
93
102
|
* using methods like `mockImplementation` or `mockImplementationOnce`.
|
|
94
103
|
*
|
|
95
104
|
* The `implementation` allows for customizing the behavior of the mock,
|
|
@@ -146,18 +155,30 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
146
155
|
*
|
|
147
156
|
* @since 1.0.0
|
|
148
157
|
*/
|
|
149
|
-
constructor(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: FunctionLikeType<void
|
|
158
|
+
constructor(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: () => FunctionLikeType<ReturnType, Args, Context> | void, name?: string);
|
|
150
159
|
/**
|
|
151
160
|
* todo remove it
|
|
152
161
|
* only for jest expect will support this mock
|
|
153
162
|
*/
|
|
154
163
|
getMockName(): string;
|
|
155
164
|
/**
|
|
156
|
-
* Retrieves the current state of
|
|
165
|
+
* Retrieves the current state of the mock function.
|
|
157
166
|
*
|
|
158
|
-
* @
|
|
159
|
-
*
|
|
167
|
+
* @remarks
|
|
168
|
+
* The `mock` getter provides read-only access to the complete state tracking information
|
|
169
|
+
* for the mock function. This includes all recorded invocations, return values, contexts,
|
|
170
|
+
* and other mock-related tracking data.
|
|
171
|
+
*
|
|
172
|
+
* Responsibilities:
|
|
173
|
+
* - Providing access to recorded function calls via {@link MocksStateInterface.calls}
|
|
174
|
+
* - Tracking invocation results through {@link MocksStateInterface.results}
|
|
175
|
+
* - Maintaining context information in {@link MocksStateInterface.contexts}
|
|
176
|
+
* - Recording instance creation in {@link MocksStateInterface.instances}
|
|
177
|
+
* - Preserving invocation order via {@link MocksStateInterface.invocationCallOrder}
|
|
178
|
+
*
|
|
179
|
+
* @returns A read-only view of the mock state tracking object.
|
|
160
180
|
*
|
|
181
|
+
* @see MocksStateInterface
|
|
161
182
|
* @since 1.0.0
|
|
162
183
|
*/
|
|
163
184
|
get mock(): Readonly<MocksStateInterface<ReturnType, Args, Context>>;
|
|
@@ -205,7 +226,7 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
205
226
|
* @returns The current instance of the mock, allowing for method chaining.
|
|
206
227
|
*
|
|
207
228
|
* @remarks
|
|
208
|
-
* The `mockReset` method clears all invocation data and results by calling `mockClear()
|
|
229
|
+
* The `mockReset` method clears all invocation data and results by calling `mockClear()` and also resets
|
|
209
230
|
* the queued implementations,
|
|
210
231
|
* removing any previously queued behavior set by methods like `mockImplementationOnce`.
|
|
211
232
|
* This ensures that the mock is in a clean state and ready for new invocations or configurations.
|
|
@@ -239,9 +260,9 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
239
260
|
/**
|
|
240
261
|
* Retrieves the mock implementation for a function, if available.
|
|
241
262
|
*
|
|
242
|
-
* @template ReturnType The type
|
|
263
|
+
* @template ReturnType The type the return value of the function.
|
|
243
264
|
* @template Context The type of the `this` context for the function.
|
|
244
|
-
* @template Args The type
|
|
265
|
+
* @template Args The type the argument(s) of the function.
|
|
245
266
|
*
|
|
246
267
|
* @return A function matching `FunctionLikeType` that represents the mock implementation,
|
|
247
268
|
* or `undefined` if no implementation is set.
|
|
@@ -430,7 +451,7 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
430
451
|
* // Set default return value
|
|
431
452
|
* mockFn.mockReturnValue('Default Value');
|
|
432
453
|
*
|
|
433
|
-
* // Set one-time return value for the next call
|
|
454
|
+
* // Set a one-time return value for the next call
|
|
434
455
|
* mockFn.mockReturnValueOnce('First Call');
|
|
435
456
|
* mockFn.mockReturnValueOnce('Second Call');
|
|
436
457
|
*
|
|
@@ -492,7 +513,7 @@ declare class MockState<ReturnType = unknown, Args extends Array<unknown> = unkn
|
|
|
492
513
|
* // Set default rejected value
|
|
493
514
|
* mockFn.mockRejectedValue('Default Error');
|
|
494
515
|
*
|
|
495
|
-
* // Set one-time rejected value for the next call
|
|
516
|
+
* // Set a one-time rejected value for the next call
|
|
496
517
|
* mockFn.mockRejectedValueOnce('First Call Error');
|
|
497
518
|
*
|
|
498
519
|
* mockFn().catch(error => {
|
|
@@ -2884,23 +2905,92 @@ declare function getTrimmedStackString(position?: number): string;
|
|
|
2884
2905
|
/**
|
|
2885
2906
|
* Imports
|
|
2886
2907
|
*/
|
|
2908
|
+
declare const proxyRegistry: WeakMap<object, {
|
|
2909
|
+
proxy: unknown;
|
|
2910
|
+
spies: Map<PropertyKey, MockState>;
|
|
2911
|
+
}>;
|
|
2887
2912
|
/**
|
|
2888
|
-
*
|
|
2913
|
+
* Checks if a property on an object is provided via a proxy mechanism rather than directly defined.
|
|
2889
2914
|
*
|
|
2890
|
-
* @template
|
|
2891
|
-
* @template Key - The key of the property to be mocked.
|
|
2915
|
+
* @template T - The type of object being checked
|
|
2892
2916
|
*
|
|
2893
|
-
* @param
|
|
2894
|
-
* @param key - The property key on the
|
|
2895
|
-
* @
|
|
2917
|
+
* @param obj - The object to inspect
|
|
2918
|
+
* @param key - The property key to check on the object
|
|
2919
|
+
* @returns `true` if the property is provided by a proxy, `false` if directly defined
|
|
2896
2920
|
*
|
|
2897
2921
|
* @remarks
|
|
2898
|
-
* This function
|
|
2899
|
-
*
|
|
2922
|
+
* This function determines whether a property on an object is being provided through
|
|
2923
|
+
* a proxy mechanism (like a Proxy object or getter) rather than being directly defined
|
|
2924
|
+
* on the object itself. It works by checking if the key doesn't exist in the object's
|
|
2925
|
+
* own properties while still returning a non-undefined value when accessed.
|
|
2900
2926
|
*
|
|
2901
|
-
*
|
|
2927
|
+
* This is useful for:
|
|
2928
|
+
* - Detecting dynamically created properties
|
|
2929
|
+
* - Identifying properties provided via getters or proxies
|
|
2930
|
+
* - Distinguishing between direct properties and inherited/proxied ones
|
|
2931
|
+
*
|
|
2932
|
+
* @example
|
|
2933
|
+
* ```ts
|
|
2934
|
+
* // Regular object with direct property
|
|
2935
|
+
* const directObj = { name: 'Test' };
|
|
2936
|
+
* console.log(isProxyProperty(directObj, 'name')); // false
|
|
2937
|
+
*
|
|
2938
|
+
* // Object with proxy property
|
|
2939
|
+
* const handler = {
|
|
2940
|
+
* get(target, prop) {
|
|
2941
|
+
* if (prop === 'dynamic') return 'This is dynamic';
|
|
2942
|
+
* return target[prop];
|
|
2943
|
+
* }
|
|
2944
|
+
* };
|
|
2945
|
+
* const proxyObj = new Proxy({}, handler);
|
|
2946
|
+
* console.log(isProxyProperty(proxyObj, 'dynamic')); // true
|
|
2947
|
+
* ```
|
|
2948
|
+
*
|
|
2949
|
+
* @since 1.2.0
|
|
2950
|
+
*/
|
|
2951
|
+
declare function isProxyProperty<T extends object>(obj: T, key: keyof T): boolean;
|
|
2952
|
+
/**
|
|
2953
|
+
* Creates a spy on a property accessed via a proxy getter, allowing interception
|
|
2954
|
+
* and monitoring of property access operations.
|
|
2955
|
+
*
|
|
2956
|
+
* @template T - The type of the target object containing the proxy
|
|
2957
|
+
* @template K - The type of property key being spied on
|
|
2958
|
+
*
|
|
2959
|
+
* @param target - The object containing the property to spy on
|
|
2960
|
+
* @param key - The property key to intercept access to
|
|
2961
|
+
* @param method - The method to execute when the property is accessed
|
|
2962
|
+
* @returns A {@link MockState} instance that tracks interactions with the property
|
|
2963
|
+
*
|
|
2964
|
+
* @throws Error - If the target is not part of any global object
|
|
2965
|
+
*
|
|
2966
|
+
* @example
|
|
2967
|
+
* ```ts
|
|
2968
|
+
* // Create an object with dynamic property access
|
|
2969
|
+
* const user = new Proxy({}, {
|
|
2970
|
+
* get(target, prop) {
|
|
2971
|
+
* if (prop === 'name') return 'John';
|
|
2972
|
+
* return target[prop];
|
|
2973
|
+
* }
|
|
2974
|
+
* });
|
|
2975
|
+
*
|
|
2976
|
+
* // Spy on the 'name' property
|
|
2977
|
+
* const nameSpy = spyOnProxyGet(user, 'name', () => 'Jane');
|
|
2978
|
+
*
|
|
2979
|
+
* // Now accessing user.name returns 'Jane' and the access is tracked
|
|
2980
|
+
* console.log(user.name); // 'Jane'
|
|
2981
|
+
* expect(nameSpy).toHaveBeenCalled();
|
|
2982
|
+
*
|
|
2983
|
+
* // Restore original behavior
|
|
2984
|
+
* nameSpy.mockRestore();
|
|
2985
|
+
* console.log(user.name); // 'John'
|
|
2986
|
+
* ```
|
|
2987
|
+
*
|
|
2988
|
+
* @see MockState
|
|
2989
|
+
* @see getParentObject
|
|
2990
|
+
*
|
|
2991
|
+
* @since 1.2.0
|
|
2902
2992
|
*/
|
|
2903
|
-
declare function
|
|
2993
|
+
declare function spyOnProxyGet<T extends object, K extends keyof T>(target: T, key: K, method: unknown): MockState;
|
|
2904
2994
|
/**
|
|
2905
2995
|
* Creates a spy on a specified static method or static property of a target class (not a class instance).
|
|
2906
2996
|
* Useful for mocking behavior during testing.
|
|
@@ -3124,6 +3214,243 @@ type ConstructorKeysType<T> = RemoveIndexType<keyof PropertiesWithConstructorsTy
|
|
|
3124
3214
|
*/
|
|
3125
3215
|
type KeysExtendingConstructorType<T> = T extends ConstructorType ? keyof RemoveIndexType<T> : never;
|
|
3126
3216
|
|
|
3217
|
+
/**
|
|
3218
|
+
* Import will remove at compile time
|
|
3219
|
+
*/
|
|
3220
|
+
/**
|
|
3221
|
+
* Imports
|
|
3222
|
+
*/
|
|
3223
|
+
/**
|
|
3224
|
+
* Finds the parent object and name of a given function or value in the global scope.
|
|
3225
|
+
*
|
|
3226
|
+
* @param fn - The function or value to find in the global scope
|
|
3227
|
+
* @returns An object containing the name and parent object of the function, or `undefined` if not found
|
|
3228
|
+
*
|
|
3229
|
+
* @remarks
|
|
3230
|
+
* The `getParentObject` function attempts to locate where a function or value is defined
|
|
3231
|
+
* within the global context (`globalThis`). It searches for the function's name in the
|
|
3232
|
+
* global scope and also within properties of global objects. This is useful for
|
|
3233
|
+
* determining the original location of a function or value in the global namespace.
|
|
3234
|
+
*
|
|
3235
|
+
* Responsibilities:
|
|
3236
|
+
* - Finding functions directly attached to `globalThis`
|
|
3237
|
+
* - Locating functions within objects in the global scope
|
|
3238
|
+
* - Identifying functions by reference equality with global properties
|
|
3239
|
+
* - Supporting both named and anonymous functions
|
|
3240
|
+
*
|
|
3241
|
+
* @example
|
|
3242
|
+
* ```ts
|
|
3243
|
+
* // Finding a global function
|
|
3244
|
+
* const result = getParentObject(setTimeout);
|
|
3245
|
+
* // Returns: { name: 'setTimeout', parent: globalThis }
|
|
3246
|
+
*
|
|
3247
|
+
* // Finding a method on a global object
|
|
3248
|
+
* const arrayResult = getParentObject(Array.prototype.map);
|
|
3249
|
+
* // Returns: { name: 'map', parent: Array.prototype }
|
|
3250
|
+
* ```
|
|
3251
|
+
*
|
|
3252
|
+
* @since 1.2.0
|
|
3253
|
+
*/
|
|
3254
|
+
declare function getParentObject(fn: unknown): {
|
|
3255
|
+
name: string;
|
|
3256
|
+
object: Record<string, unknown>;
|
|
3257
|
+
} | undefined;
|
|
3258
|
+
/**
|
|
3259
|
+
* Creates a mock for an object property using property descriptors.
|
|
3260
|
+
*
|
|
3261
|
+
* @template T - The type of the target object containing the property to mock
|
|
3262
|
+
*
|
|
3263
|
+
* @param target - The object containing the property to mock
|
|
3264
|
+
* @param key - The name of the property to mock
|
|
3265
|
+
* @returns A {@link MockState} instance that tracks interactions with the property
|
|
3266
|
+
*
|
|
3267
|
+
* @remarks
|
|
3268
|
+
* The `mockDescriptorProperty` function replaces a property on a target object with a getter/setter
|
|
3269
|
+
* that intercepts access to that property. This allows for monitoring and controlling property
|
|
3270
|
+
* access during tests. The original property can be restored later through the mock's
|
|
3271
|
+
* restore capability.
|
|
3272
|
+
*
|
|
3273
|
+
* Responsibilities:
|
|
3274
|
+
* - Intercepting property access via custom property descriptors
|
|
3275
|
+
* - Capturing the original property value and descriptor
|
|
3276
|
+
* - Creating a {@link MockState} instance to track interactions
|
|
3277
|
+
* - Supporting property restoration through the {@link MockState.mockRestore} method
|
|
3278
|
+
* - Maintaining references in the global {@link MockState.mocks} registry
|
|
3279
|
+
*
|
|
3280
|
+
* @example
|
|
3281
|
+
* ```ts
|
|
3282
|
+
* // Mock a property on an object
|
|
3283
|
+
* const obj = { value: 42 };
|
|
3284
|
+
* const mockValue = mockDescriptorProperty(obj, 'value');
|
|
3285
|
+
* ```
|
|
3286
|
+
*
|
|
3287
|
+
* @see MockState
|
|
3288
|
+
* @since 1.2.0
|
|
3289
|
+
*/
|
|
3290
|
+
declare function mockDescriptorProperty<T extends object>(target: T, key: string | number | symbol): MockState;
|
|
3291
|
+
/**
|
|
3292
|
+
* Creates a mock function interface with the specified implementation and optional restore function.
|
|
3293
|
+
*
|
|
3294
|
+
* @template ReturnType - The return type of the mocked function.
|
|
3295
|
+
* @template Context - The context type that the mocked function binds to.
|
|
3296
|
+
* @template Args - The argument type of the mocked function. Defaults to an array of unknown values.
|
|
3297
|
+
*
|
|
3298
|
+
* @param implementation - An optional implementation of the mocked function.
|
|
3299
|
+
* @param restore - An optional restore function used to reset the mock.
|
|
3300
|
+
* @returns A mocked function interface with the specified behaviors.
|
|
3301
|
+
*
|
|
3302
|
+
* @remarks
|
|
3303
|
+
* The `fnImplementation` function creates a mock function handler, typically used in testing scenarios.
|
|
3304
|
+
* It transforms regular functions into mockable objects that can be monitored and controlled.
|
|
3305
|
+
*
|
|
3306
|
+
* Responsibilities:
|
|
3307
|
+
* - Creating mock functions with custom implementations
|
|
3308
|
+
* - Supporting restore functionality for resetting mocks
|
|
3309
|
+
* - Providing type-safe mock interfaces via {@link FnMockInterface}
|
|
3310
|
+
* - Integrating with the {@link MockState} system
|
|
3311
|
+
*
|
|
3312
|
+
* @example
|
|
3313
|
+
* ```ts
|
|
3314
|
+
* // Creating a mock with a custom implementation
|
|
3315
|
+
* const mock = xJet.fn((x: number) => x * 2);
|
|
3316
|
+
* console.log(mock(5)); // 10
|
|
3317
|
+
*
|
|
3318
|
+
* // Creating a mock with a restore function
|
|
3319
|
+
* const mockWithRestore = xJet.fn(undefined, () => { console.log('Restored!'); });
|
|
3320
|
+
* mockWithRestore.restore(); // "Restored!"
|
|
3321
|
+
* ```
|
|
3322
|
+
*
|
|
3323
|
+
* @see MockState
|
|
3324
|
+
* @see FnMockInterface
|
|
3325
|
+
* @see FunctionLikeType
|
|
3326
|
+
*
|
|
3327
|
+
* @since 1.2.0
|
|
3328
|
+
*/
|
|
3329
|
+
declare function fnImplementation<ReturnType, Args extends Array<unknown>, Context>(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: () => FunctionLikeType<ReturnType, Args, Context> | void): FnMockInterface<ReturnType, Args, Context>;
|
|
3330
|
+
/**
|
|
3331
|
+
* Creates a mock function with an optional custom implementation.
|
|
3332
|
+
*
|
|
3333
|
+
* @template Method - The return type of the function being mocked
|
|
3334
|
+
* @template Args - The argument types for the function, defaulting to unknown array
|
|
3335
|
+
* @template Context - The context type (`this`) for the function, defaulting to unknown
|
|
3336
|
+
*
|
|
3337
|
+
* @param method - The original function to mock
|
|
3338
|
+
* @param implementation - Optional custom implementation to use instead of the original
|
|
3339
|
+
* @returns A {@link MockState} instance that wraps the original function
|
|
3340
|
+
*
|
|
3341
|
+
* @remarks
|
|
3342
|
+
* The `mockImplementation` function creates a new {@link MockState} instance that wraps
|
|
3343
|
+
* around a provided function, allowing you to monitor calls to that function and optionally
|
|
3344
|
+
* override its implementation. This is particularly useful for testing components that
|
|
3345
|
+
* depend on external functions by providing controlled behavior during tests.
|
|
3346
|
+
*
|
|
3347
|
+
* Responsibilities:
|
|
3348
|
+
* - Creating a trackable mock function from any original function
|
|
3349
|
+
* - Supporting custom implementation override capability
|
|
3350
|
+
* - Preserving the original function's signature and return type
|
|
3351
|
+
* - Enabling all mock functionality like call tracking and verification
|
|
3352
|
+
* - Maintaining type safety between the original and mocked functions
|
|
3353
|
+
*
|
|
3354
|
+
* @example
|
|
3355
|
+
* ```ts
|
|
3356
|
+
* // Mock a function with default implementation
|
|
3357
|
+
* const fetchData = async () => ({ id: 1, name: 'Test' });
|
|
3358
|
+
* const mockedFetch = xJet.mock(fetchData);
|
|
3359
|
+
*
|
|
3360
|
+
* // Mock with custom implementation
|
|
3361
|
+
* const mockedFetchCustom = xJet.mock(fetchData, async () => {
|
|
3362
|
+
* return { id: 2, name: 'Custom Test' };
|
|
3363
|
+
* });
|
|
3364
|
+
*
|
|
3365
|
+
* test('uses mocked function', async () => {
|
|
3366
|
+
* const result = await mockedFetchCustom();
|
|
3367
|
+
* expect(result.name).toBe('Custom Test');
|
|
3368
|
+
* expect(mockedFetchCustom).toHaveBeenCalled();
|
|
3369
|
+
* });
|
|
3370
|
+
* ```
|
|
3371
|
+
*
|
|
3372
|
+
* @see MockState
|
|
3373
|
+
* @see FunctionLikeType
|
|
3374
|
+
* @see ConstructorLikeType
|
|
3375
|
+
*
|
|
3376
|
+
* @since 1.2.0
|
|
3377
|
+
*/
|
|
3378
|
+
declare function mockImplementation<Method, Args extends Array<unknown> = [], Context = unknown>(method: FunctionLikeType<Method, Args, Context> | ConstructorLikeType<Method, Args>, implementation?: FunctionLikeType<Method, Args, Context>): MockState<Method, Args, Context>;
|
|
3379
|
+
/**
|
|
3380
|
+
* Creates a mock for an element with an optional custom implementation.
|
|
3381
|
+
*
|
|
3382
|
+
* @template Element - The type of the element being mocked
|
|
3383
|
+
*
|
|
3384
|
+
* @param item - The element to mock
|
|
3385
|
+
* @param implementation - Optional custom implementation to replace the original element's behavior
|
|
3386
|
+
* @returns A {@link MockState} instance that controls and tracks the mock
|
|
3387
|
+
*
|
|
3388
|
+
* @remarks
|
|
3389
|
+
* The `mockImplementation` function creates a new {@link MockState} instance that wraps
|
|
3390
|
+
* around the provided element, allowing you to observe interactions with it and optionally
|
|
3391
|
+
* override its behavior. This is useful for isolating components during testing by
|
|
3392
|
+
* replacing their dependencies with controlled mocks.
|
|
3393
|
+
*
|
|
3394
|
+
* Responsibilities:
|
|
3395
|
+
* - Creating a trackable mock from any element
|
|
3396
|
+
* - Supporting custom implementation substitution
|
|
3397
|
+
* - Maintaining type safety between the original and mocked elements
|
|
3398
|
+
* - Enabling interaction tracking and verification capabilities
|
|
3399
|
+
* - Providing a fluent API for configuring mock behavior
|
|
3400
|
+
*
|
|
3401
|
+
* @example
|
|
3402
|
+
* ```ts
|
|
3403
|
+
* // Mock a simple value like export const testValue = 'original value'
|
|
3404
|
+
* const mockValue = xJet.mock(testValue);
|
|
3405
|
+
* mockValue.mockReturnValue("mocked value");
|
|
3406
|
+
*
|
|
3407
|
+
* // Mock a function
|
|
3408
|
+
* const originalFn = (name: string) => `Hello, ${name}!`;
|
|
3409
|
+
* const mockedFn = xJet.mock(originalFn);
|
|
3410
|
+
*
|
|
3411
|
+
* // Configure custom implementation
|
|
3412
|
+
* mockedFn.mockImplementation((name: string) => `Hi, ${name}!`);
|
|
3413
|
+
*
|
|
3414
|
+
* test ('uses mocked function', () => {
|
|
3415
|
+
* const result = xJet.mock(testValue);
|
|
3416
|
+
* expect(result).toBe('Hi, World!');
|
|
3417
|
+
* expect(mockedFn).toHaveBeenCalledWith('World');
|
|
3418
|
+
* });
|
|
3419
|
+
* ```
|
|
3420
|
+
*
|
|
3421
|
+
* @see MockState
|
|
3422
|
+
* @see FunctionLikeType
|
|
3423
|
+
*
|
|
3424
|
+
* @since 1.2.0
|
|
3425
|
+
*/
|
|
3426
|
+
declare function mockImplementation<Element = unknown>(item: Element, implementation?: FunctionLikeType<Element>): MockState<Element>;
|
|
3427
|
+
|
|
3428
|
+
/**
|
|
3429
|
+
* Import will remove at compile time
|
|
3430
|
+
*/
|
|
3431
|
+
/**
|
|
3432
|
+
* Represents a mockable function interface with a customizable return type, context,
|
|
3433
|
+
* and argument list. This interface extends `MockState` to facilitate tracking
|
|
3434
|
+
* and testing of function behaviors and states.
|
|
3435
|
+
*
|
|
3436
|
+
* @template ReturnType - Specifies the return type of the function. Defaults to `unknown`.
|
|
3437
|
+
* @template Context - Defines the function's "this" context type. Defaults to `unknown`.
|
|
3438
|
+
* @template Args - Sets the argument type(s) for the function, represented as an array. Defaults to `unknown[]`.
|
|
3439
|
+
*
|
|
3440
|
+
* @remarks
|
|
3441
|
+
* This interface is useful for creating test doubles or mock implementations that simulate
|
|
3442
|
+
* complex behaviors (allows for both `function-like` behavior and `constructor-like` behavior)
|
|
3443
|
+
* while tracking interactions and state information.
|
|
3444
|
+
*
|
|
3445
|
+
* @see MockState
|
|
3446
|
+
*
|
|
3447
|
+
* @since 1.0.0
|
|
3448
|
+
*/
|
|
3449
|
+
interface FnMockInterface<ReturnType = unknown, Args extends Array<unknown> = any, Context = any> extends MockState<ReturnType, Args, Context> {
|
|
3450
|
+
new (...args: Args): ReturnType;
|
|
3451
|
+
(this: Context, ...args: Args): ReturnType;
|
|
3452
|
+
}
|
|
3453
|
+
|
|
3127
3454
|
/**
|
|
3128
3455
|
* Import will remove at compile time
|
|
3129
3456
|
*/
|
|
@@ -4777,132 +5104,6 @@ interface DescribeDirectiveInterface {
|
|
|
4777
5104
|
invoke(description: string, block: FunctionType, args?: Array<unknown>): void;
|
|
4778
5105
|
}
|
|
4779
5106
|
|
|
4780
|
-
/**
|
|
4781
|
-
* Import will remove at compile time
|
|
4782
|
-
*/
|
|
4783
|
-
/**
|
|
4784
|
-
* Imports
|
|
4785
|
-
*/
|
|
4786
|
-
/**
|
|
4787
|
-
* Retrieves the parent object of the specified function if it exists within the global context.
|
|
4788
|
-
*
|
|
4789
|
-
* @template FunctionLikeType - The type representing the input function.
|
|
4790
|
-
*
|
|
4791
|
-
* @param fn - The function whose parent object is to be retrieved.
|
|
4792
|
-
* @returns The parent object containing the function, or `undefined` if no such object is found.
|
|
4793
|
-
*
|
|
4794
|
-
* @remarks
|
|
4795
|
-
* This method searches for the parent object of a given function within the global context
|
|
4796
|
-
* by checking if the function name exists as a key of an object in the global scope.
|
|
4797
|
-
* If the function exists directly in the global scope, the global object itself is returned.
|
|
4798
|
-
*
|
|
4799
|
-
* @since 1.0.0
|
|
4800
|
-
*/
|
|
4801
|
-
declare function getParentObject(fn: FunctionLikeType): Record<string, unknown> | undefined;
|
|
4802
|
-
/**
|
|
4803
|
-
* Creates a mock function interface with the specified implementation and optional restore function.
|
|
4804
|
-
*
|
|
4805
|
-
* @template ReturnType - The return type of the mocked function.
|
|
4806
|
-
* @template Context - The context type that the mocked function binds to.
|
|
4807
|
-
* @template Args - The argument type of the mocked function. Defaults to an array of unknown values.
|
|
4808
|
-
*
|
|
4809
|
-
* @param implementation - An optional implementation of the mocked function.
|
|
4810
|
-
* @param restore - An optional restore function used to reset the mock.
|
|
4811
|
-
* @returns A mocked function interface with the specified behaviors.
|
|
4812
|
-
*
|
|
4813
|
-
* @remarks
|
|
4814
|
-
* This function creates a mock function handler, typically used in testing scenarios.
|
|
4815
|
-
* You can provide an implementation for the mock behavior or specify a restore
|
|
4816
|
-
* handler for resetting the mock's state.
|
|
4817
|
-
*
|
|
4818
|
-
* @example
|
|
4819
|
-
* ```ts
|
|
4820
|
-
* // Creating a mock with a custom implementation
|
|
4821
|
-
* const mock = xJet.fn((x: number) => x * 2);
|
|
4822
|
-
* console.log(mock(5)); // 10
|
|
4823
|
-
*
|
|
4824
|
-
* // Creating a mock with a restore function
|
|
4825
|
-
* const mockWithRestore = xJet.fnImplementation(undefined, () => { console.log('Restored!'); });
|
|
4826
|
-
* mockWithRestore.restore(); // "Restored!"
|
|
4827
|
-
* ```
|
|
4828
|
-
*
|
|
4829
|
-
* @see MockState
|
|
4830
|
-
*
|
|
4831
|
-
* @since 1.0.0
|
|
4832
|
-
*/
|
|
4833
|
-
declare function fnImplementation<ReturnType, Args extends Array<unknown>, Context>(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: FunctionLikeType<void>): FnMockInterface<ReturnType, Args, Context>;
|
|
4834
|
-
/**
|
|
4835
|
-
* Creates a mock instance for the given method or constructor.
|
|
4836
|
-
*
|
|
4837
|
-
* @template Method - The type of the method being mocked.
|
|
4838
|
-
* @template Context - The `this` context type of the method.
|
|
4839
|
-
* @template Args - The types of the arguments accepted by the method.
|
|
4840
|
-
*
|
|
4841
|
-
* @param method - The method or constructor to mock. This can either be a function-like type or a
|
|
4842
|
-
* constructor-like type.
|
|
4843
|
-
* @returns A `MockState` instance associated with the provided method, allowing for capturing
|
|
4844
|
-
* interactions and controlling behavior during testing.
|
|
4845
|
-
*
|
|
4846
|
-
* @remarks
|
|
4847
|
-
* This method identifies whether the provided method is a function or constructor and creates
|
|
4848
|
-
* a suitable mock state. If the method is already mocked, the existing mock state is returned.
|
|
4849
|
-
* Throws an error if the method does not belong to an object or if it has an invalid type.
|
|
4850
|
-
*
|
|
4851
|
-
* @example
|
|
4852
|
-
* ```ts
|
|
4853
|
-
* // Mocking a regular method
|
|
4854
|
-
* function greet(name: string) {
|
|
4855
|
-
* return `Hello, ${ name }`;
|
|
4856
|
-
* }
|
|
4857
|
-
*
|
|
4858
|
-
* const greetMock = xJet.mock(greet);
|
|
4859
|
-
* greetMock.mockImplementation(() => 'Hi!');
|
|
4860
|
-
* console.log(greet('World')); // "Hi!"
|
|
4861
|
-
*
|
|
4862
|
-
* // Mocking a constructor
|
|
4863
|
-
* class Person {
|
|
4864
|
-
* constructor(public name: string) {}
|
|
4865
|
-
* }
|
|
4866
|
-
* const personMock = xJet.mock(Person);
|
|
4867
|
-
* personMock.mockImplementation((name: string) => ({ name: `${name} (mocked)` }));
|
|
4868
|
-
* const person = new Person('Alice');
|
|
4869
|
-
* console.log(person.name); // "Alice (mocked)"
|
|
4870
|
-
*
|
|
4871
|
-
* // Restoring the original method
|
|
4872
|
-
* greetMock.mockRestore();
|
|
4873
|
-
* console.log(greet('World')); // "Hello, World"
|
|
4874
|
-
* ```
|
|
4875
|
-
*
|
|
4876
|
-
* @since 1.0.0
|
|
4877
|
-
*/
|
|
4878
|
-
declare function mockImplementation<Method, Args extends Array<unknown>, Context>(method: FunctionLikeType<Method, Args, Context> | ConstructorLikeType<Method, Args>): MockState<Method, Args, Context>;
|
|
4879
|
-
|
|
4880
|
-
/**
|
|
4881
|
-
* Import will remove at compile time
|
|
4882
|
-
*/
|
|
4883
|
-
/**
|
|
4884
|
-
* Represents a mockable function interface with a customizable return type, context,
|
|
4885
|
-
* and argument list. This interface extends `MockState` to facilitate tracking
|
|
4886
|
-
* and testing of function behaviors and states.
|
|
4887
|
-
*
|
|
4888
|
-
* @template ReturnType - Specifies the return type of the function. Defaults to `unknown`.
|
|
4889
|
-
* @template Context - Defines the function's "this" context type. Defaults to `unknown`.
|
|
4890
|
-
* @template Args - Sets the argument type(s) for the function, represented as an array. Defaults to `unknown[]`.
|
|
4891
|
-
*
|
|
4892
|
-
* @remarks
|
|
4893
|
-
* This interface is useful for creating test doubles or mock implementations that simulate
|
|
4894
|
-
* complex behaviors (allows for both `function-like` behavior and `constructor-like` behavior)
|
|
4895
|
-
* while tracking interactions and state information.
|
|
4896
|
-
*
|
|
4897
|
-
* @see MockState
|
|
4898
|
-
*
|
|
4899
|
-
* @since 1.0.0
|
|
4900
|
-
*/
|
|
4901
|
-
interface FnMockInterface<ReturnType = unknown, Args extends Array<unknown> = any, Context = any> extends MockState<ReturnType, Args, Context> {
|
|
4902
|
-
new (...args: Args): ReturnType;
|
|
4903
|
-
(this: Context, ...args: Args): ReturnType;
|
|
4904
|
-
}
|
|
4905
|
-
|
|
4906
5107
|
/**
|
|
4907
5108
|
* Import will remove at compile time
|
|
4908
5109
|
*/
|