@remotex-labs/xjet 1.1.0 → 1.2.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/README.md +39 -25
- package/dist/bash.js +2 -2
- package/dist/bash.js.map +3 -3
- 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 +15 -11
package/dist/index.d.ts
CHANGED
|
@@ -77,21 +77,73 @@ export {};
|
|
|
77
77
|
* Imports
|
|
78
78
|
*/
|
|
79
79
|
/**
|
|
80
|
-
*
|
|
80
|
+
* Finds the parent object and name of a given function or value in the global scope.
|
|
81
81
|
*
|
|
82
|
-
* @
|
|
82
|
+
* @param fn - The function or value to find in the global scope
|
|
83
|
+
* @returns An object containing the name and parent object of the function, or `undefined` if not found
|
|
83
84
|
*
|
|
84
|
-
* @
|
|
85
|
-
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* The `getParentObject` function attempts to locate where a function or value is defined
|
|
87
|
+
* within the global context (`globalThis`). It searches for the function's name in the
|
|
88
|
+
* global scope and also within properties of global objects. This is useful for
|
|
89
|
+
* determining the original location of a function or value in the global namespace.
|
|
90
|
+
*
|
|
91
|
+
* Responsibilities:
|
|
92
|
+
* - Finding functions directly attached to `globalThis`
|
|
93
|
+
* - Locating functions within objects in the global scope
|
|
94
|
+
* - Identifying functions by reference equality with global properties
|
|
95
|
+
* - Supporting both named and anonymous functions
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* // Finding a global function
|
|
100
|
+
* const result = getParentObject(setTimeout);
|
|
101
|
+
* // Returns: { name: 'setTimeout', parent: globalThis }
|
|
102
|
+
*
|
|
103
|
+
* // Finding a method on a global object
|
|
104
|
+
* const arrayResult = getParentObject(Array.prototype.map);
|
|
105
|
+
* // Returns: { name: 'map', parent: Array.prototype }
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @since 1.2.0
|
|
109
|
+
*/
|
|
110
|
+
declare function getParentObject(fn: unknown): {
|
|
111
|
+
name: string;
|
|
112
|
+
object: Record<string, unknown>;
|
|
113
|
+
} | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* Creates a mock for an object property using property descriptors.
|
|
116
|
+
*
|
|
117
|
+
* @template T - The type of the target object containing the property to mock
|
|
118
|
+
*
|
|
119
|
+
* @param target - The object containing the property to mock
|
|
120
|
+
* @param key - The name of the property to mock
|
|
121
|
+
* @returns A {@link MockState} instance that tracks interactions with the property
|
|
86
122
|
*
|
|
87
123
|
* @remarks
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
124
|
+
* The `mockDescriptorProperty` function replaces a property on a target object with a getter/setter
|
|
125
|
+
* that intercepts access to that property. This allows for monitoring and controlling property
|
|
126
|
+
* access during tests. The original property can be restored later through the mock's
|
|
127
|
+
* restore capability.
|
|
128
|
+
*
|
|
129
|
+
* Responsibilities:
|
|
130
|
+
* - Intercepting property access via custom property descriptors
|
|
131
|
+
* - Capturing the original property value and descriptor
|
|
132
|
+
* - Creating a {@link MockState} instance to track interactions
|
|
133
|
+
* - Supporting property restoration through the {@link MockState.mockRestore} method
|
|
134
|
+
* - Maintaining references in the global {@link MockState.mocks} registry
|
|
91
135
|
*
|
|
92
|
-
* @
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* // Mock a property on an object
|
|
139
|
+
* const obj = { value: 42 };
|
|
140
|
+
* const mockValue = mockDescriptorProperty(obj, 'value');
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @see MockState
|
|
144
|
+
* @since 1.2.0
|
|
93
145
|
*/
|
|
94
|
-
declare function
|
|
146
|
+
declare function mockDescriptorProperty<T extends object>(target: T, key: string | number | symbol): MockState;
|
|
95
147
|
/**
|
|
96
148
|
* Creates a mock function interface with the specified implementation and optional restore function.
|
|
97
149
|
*
|
|
@@ -104,9 +156,14 @@ declare function getParentObject(fn: FunctionLikeType): Record<string, unknown>
|
|
|
104
156
|
* @returns A mocked function interface with the specified behaviors.
|
|
105
157
|
*
|
|
106
158
|
* @remarks
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
159
|
+
* The `fnImplementation` function creates a mock function handler, typically used in testing scenarios.
|
|
160
|
+
* It transforms regular functions into mockable objects that can be monitored and controlled.
|
|
161
|
+
*
|
|
162
|
+
* Responsibilities:
|
|
163
|
+
* - Creating mock functions with custom implementations
|
|
164
|
+
* - Supporting restore functionality for resetting mocks
|
|
165
|
+
* - Providing type-safe mock interfaces via {@link FnMockInterface}
|
|
166
|
+
* - Integrating with the {@link MockState} system
|
|
110
167
|
*
|
|
111
168
|
* @example
|
|
112
169
|
* ```ts
|
|
@@ -115,60 +172,114 @@ declare function getParentObject(fn: FunctionLikeType): Record<string, unknown>
|
|
|
115
172
|
* console.log(mock(5)); // 10
|
|
116
173
|
*
|
|
117
174
|
* // Creating a mock with a restore function
|
|
118
|
-
* const mockWithRestore = xJet.
|
|
175
|
+
* const mockWithRestore = xJet.fn(undefined, () => { console.log('Restored!'); });
|
|
119
176
|
* mockWithRestore.restore(); // "Restored!"
|
|
120
177
|
* ```
|
|
121
178
|
*
|
|
122
179
|
* @see MockState
|
|
180
|
+
* @see FnMockInterface
|
|
181
|
+
* @see FunctionLikeType
|
|
123
182
|
*
|
|
124
|
-
* @since 1.
|
|
183
|
+
* @since 1.2.0
|
|
125
184
|
*/
|
|
126
|
-
declare function fnImplementation<ReturnType, Args extends Array<unknown>, Context>(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: FunctionLikeType<void
|
|
185
|
+
declare function fnImplementation<ReturnType, Args extends Array<unknown>, Context>(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: () => FunctionLikeType<ReturnType, Args, Context> | void): FnMockInterface<ReturnType, Args, Context>;
|
|
127
186
|
/**
|
|
128
|
-
* Creates a mock
|
|
187
|
+
* Creates a mock function with an optional custom implementation.
|
|
129
188
|
*
|
|
130
|
-
* @template Method - The type of the
|
|
131
|
-
* @template
|
|
132
|
-
* @template
|
|
189
|
+
* @template Method - The return type of the function being mocked
|
|
190
|
+
* @template Args - The argument types for the function, defaulting to unknown array
|
|
191
|
+
* @template Context - The context type (`this`) for the function, defaulting to unknown
|
|
133
192
|
*
|
|
134
|
-
* @param method - The
|
|
135
|
-
*
|
|
136
|
-
* @returns A
|
|
137
|
-
* interactions and controlling behavior during testing.
|
|
193
|
+
* @param method - The original function to mock
|
|
194
|
+
* @param implementation - Optional custom implementation to use instead of the original
|
|
195
|
+
* @returns A {@link MockState} instance that wraps the original function
|
|
138
196
|
*
|
|
139
197
|
* @remarks
|
|
140
|
-
*
|
|
141
|
-
* a
|
|
142
|
-
*
|
|
198
|
+
* The `mockImplementation` function creates a new {@link MockState} instance that wraps
|
|
199
|
+
* around a provided function, allowing you to monitor calls to that function and optionally
|
|
200
|
+
* override its implementation. This is particularly useful for testing components that
|
|
201
|
+
* depend on external functions by providing controlled behavior during tests.
|
|
202
|
+
*
|
|
203
|
+
* Responsibilities:
|
|
204
|
+
* - Creating a trackable mock function from any original function
|
|
205
|
+
* - Supporting custom implementation override capability
|
|
206
|
+
* - Preserving the original function's signature and return type
|
|
207
|
+
* - Enabling all mock functionality like call tracking and verification
|
|
208
|
+
* - Maintaining type safety between the original and mocked functions
|
|
143
209
|
*
|
|
144
210
|
* @example
|
|
145
211
|
* ```ts
|
|
146
|
-
* //
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* }
|
|
212
|
+
* // Mock a function with default implementation
|
|
213
|
+
* const fetchData = async () => ({ id: 1, name: 'Test' });
|
|
214
|
+
* const mockedFetch = xJet.mock(fetchData);
|
|
150
215
|
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
216
|
+
* // Mock with custom implementation
|
|
217
|
+
* const mockedFetchCustom = xJet.mock(fetchData, async () => {
|
|
218
|
+
* return { id: 2, name: 'Custom Test' };
|
|
219
|
+
* });
|
|
154
220
|
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* personMock.mockImplementation((name: string) => ({ name: `${name} (mocked)` }));
|
|
161
|
-
* const person = new Person('Alice');
|
|
162
|
-
* console.log(person.name); // "Alice (mocked)"
|
|
163
|
-
*
|
|
164
|
-
* // Restoring the original method
|
|
165
|
-
* greetMock.mockRestore();
|
|
166
|
-
* console.log(greet('World')); // "Hello, World"
|
|
221
|
+
* test('uses mocked function', async () => {
|
|
222
|
+
* const result = await mockedFetchCustom();
|
|
223
|
+
* expect(result.name).toBe('Custom Test');
|
|
224
|
+
* expect(mockedFetchCustom).toHaveBeenCalled();
|
|
225
|
+
* });
|
|
167
226
|
* ```
|
|
168
227
|
*
|
|
169
|
-
* @
|
|
228
|
+
* @see MockState
|
|
229
|
+
* @see FunctionLikeType
|
|
230
|
+
* @see ConstructorLikeType
|
|
231
|
+
*
|
|
232
|
+
* @since 1.2.0
|
|
170
233
|
*/
|
|
171
|
-
declare function mockImplementation<Method, Args extends Array<unknown
|
|
234
|
+
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>;
|
|
235
|
+
/**
|
|
236
|
+
* Creates a mock for an element with an optional custom implementation.
|
|
237
|
+
*
|
|
238
|
+
* @template Element - The type of the element being mocked
|
|
239
|
+
*
|
|
240
|
+
* @param item - The element to mock
|
|
241
|
+
* @param implementation - Optional custom implementation to replace the original element's behavior
|
|
242
|
+
* @returns A {@link MockState} instance that controls and tracks the mock
|
|
243
|
+
*
|
|
244
|
+
* @remarks
|
|
245
|
+
* The `mockImplementation` function creates a new {@link MockState} instance that wraps
|
|
246
|
+
* around the provided element, allowing you to observe interactions with it and optionally
|
|
247
|
+
* override its behavior. This is useful for isolating components during testing by
|
|
248
|
+
* replacing their dependencies with controlled mocks.
|
|
249
|
+
*
|
|
250
|
+
* Responsibilities:
|
|
251
|
+
* - Creating a trackable mock from any element
|
|
252
|
+
* - Supporting custom implementation substitution
|
|
253
|
+
* - Maintaining type safety between the original and mocked elements
|
|
254
|
+
* - Enabling interaction tracking and verification capabilities
|
|
255
|
+
* - Providing a fluent API for configuring mock behavior
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* // Mock a simple value like export const testValue = 'original value'
|
|
260
|
+
* const mockValue = xJet.mock(testValue);
|
|
261
|
+
* mockValue.mockReturnValue("mocked value");
|
|
262
|
+
*
|
|
263
|
+
* // Mock a function
|
|
264
|
+
* const originalFn = (name: string) => `Hello, ${name}!`;
|
|
265
|
+
* const mockedFn = xJet.mock(originalFn);
|
|
266
|
+
*
|
|
267
|
+
* // Configure custom implementation
|
|
268
|
+
* mockedFn.mockImplementation((name: string) => `Hi, ${name}!`);
|
|
269
|
+
*
|
|
270
|
+
* test ('uses mocked function', () => {
|
|
271
|
+
* const result = xJet.mock(testValue);
|
|
272
|
+
* expect(result).toBe('Hi, World!');
|
|
273
|
+
* expect(mockedFn).toHaveBeenCalledWith('World');
|
|
274
|
+
* });
|
|
275
|
+
* ```
|
|
276
|
+
*
|
|
277
|
+
* @see MockState
|
|
278
|
+
* @see FunctionLikeType
|
|
279
|
+
*
|
|
280
|
+
* @since 1.2.0
|
|
281
|
+
*/
|
|
282
|
+
declare function mockImplementation<Element = unknown>(item: Element, implementation?: FunctionLikeType<Element>): MockState<Element>;
|
|
172
283
|
|
|
173
284
|
/**
|
|
174
285
|
* Import will remove at compile time
|
|
@@ -241,16 +352,25 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
241
352
|
*/
|
|
242
353
|
private state;
|
|
243
354
|
/**
|
|
244
|
-
* A private function that restores the
|
|
245
|
-
* It resets the mock to its initial state, typically used when restoring
|
|
246
|
-
* the mock after a call to `mockReset` or `mockRestore`.
|
|
355
|
+
* A private function that restores the mock's original implementation.
|
|
247
356
|
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
357
|
+
* @remarks
|
|
358
|
+
* The `restore` function is responsible for resetting the mock to its initial state.
|
|
359
|
+
* It works in conjunction with methods like `mockReset` and `mockRestore` to ensure
|
|
360
|
+
* proper restoration of the mock's behavior.
|
|
250
361
|
*
|
|
251
|
-
*
|
|
362
|
+
* Responsibilities:
|
|
363
|
+
* - Returning the original implementation when restoring the mock
|
|
364
|
+
* - Ensuring consistent reset behavior across mock operations
|
|
365
|
+
* - Supporting the {@link mockRestore} method's functionality
|
|
366
|
+
* - Maintaining mock state integrity during restoration
|
|
367
|
+
*
|
|
368
|
+
* @see MockState.mockRestore
|
|
369
|
+
* @see MockState.originalImplementation
|
|
370
|
+
*
|
|
371
|
+
* @since 1.2.0
|
|
252
372
|
*/
|
|
253
|
-
private readonly restore
|
|
373
|
+
private readonly restore?;
|
|
254
374
|
/**
|
|
255
375
|
* A private array that stores the implementations queued to be executed
|
|
256
376
|
* for future invocations of the mock.
|
|
@@ -265,7 +385,7 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
265
385
|
private queuedImplementations;
|
|
266
386
|
/**
|
|
267
387
|
* A private property that holds the current implementation of the mock function.
|
|
268
|
-
* This function is executed whenever the mock is invoked
|
|
388
|
+
* This function is executed whenever the mock is invoked and can be changed
|
|
269
389
|
* using methods like `mockImplementation` or `mockImplementationOnce`.
|
|
270
390
|
*
|
|
271
391
|
* The `implementation` allows for customizing the behavior of the mock,
|
|
@@ -322,18 +442,30 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
322
442
|
*
|
|
323
443
|
* @since 1.0.0
|
|
324
444
|
*/
|
|
325
|
-
constructor(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: FunctionLikeType<void
|
|
445
|
+
constructor(implementation?: FunctionLikeType<ReturnType, Args, Context>, restore?: () => FunctionLikeType<ReturnType, Args, Context> | void, name?: string);
|
|
326
446
|
/**
|
|
327
447
|
* todo remove it
|
|
328
448
|
* only for jest expect will support this mock
|
|
329
449
|
*/
|
|
330
450
|
getMockName(): string;
|
|
331
451
|
/**
|
|
332
|
-
* Retrieves the current state of
|
|
452
|
+
* Retrieves the current state of the mock function.
|
|
333
453
|
*
|
|
334
|
-
* @
|
|
335
|
-
*
|
|
454
|
+
* @remarks
|
|
455
|
+
* The `mock` getter provides read-only access to the complete state tracking information
|
|
456
|
+
* for the mock function. This includes all recorded invocations, return values, contexts,
|
|
457
|
+
* and other mock-related tracking data.
|
|
458
|
+
*
|
|
459
|
+
* Responsibilities:
|
|
460
|
+
* - Providing access to recorded function calls via {@link MocksStateInterface.calls}
|
|
461
|
+
* - Tracking invocation results through {@link MocksStateInterface.results}
|
|
462
|
+
* - Maintaining context information in {@link MocksStateInterface.contexts}
|
|
463
|
+
* - Recording instance creation in {@link MocksStateInterface.instances}
|
|
464
|
+
* - Preserving invocation order via {@link MocksStateInterface.invocationCallOrder}
|
|
465
|
+
*
|
|
466
|
+
* @returns A read-only view of the mock state tracking object.
|
|
336
467
|
*
|
|
468
|
+
* @see MocksStateInterface
|
|
337
469
|
* @since 1.0.0
|
|
338
470
|
*/
|
|
339
471
|
get mock(): Readonly<MocksStateInterface<ReturnType, Args, Context>>;
|
|
@@ -381,7 +513,7 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
381
513
|
* @returns The current instance of the mock, allowing for method chaining.
|
|
382
514
|
*
|
|
383
515
|
* @remarks
|
|
384
|
-
* The `mockReset` method clears all invocation data and results by calling `mockClear()
|
|
516
|
+
* The `mockReset` method clears all invocation data and results by calling `mockClear()` and also resets
|
|
385
517
|
* the queued implementations,
|
|
386
518
|
* removing any previously queued behavior set by methods like `mockImplementationOnce`.
|
|
387
519
|
* This ensures that the mock is in a clean state and ready for new invocations or configurations.
|
|
@@ -415,9 +547,9 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
415
547
|
/**
|
|
416
548
|
* Retrieves the mock implementation for a function, if available.
|
|
417
549
|
*
|
|
418
|
-
* @template ReturnType The type
|
|
550
|
+
* @template ReturnType The type the return value of the function.
|
|
419
551
|
* @template Context The type of the `this` context for the function.
|
|
420
|
-
* @template Args The type
|
|
552
|
+
* @template Args The type the argument(s) of the function.
|
|
421
553
|
*
|
|
422
554
|
* @return A function matching `FunctionLikeType` that represents the mock implementation,
|
|
423
555
|
* or `undefined` if no implementation is set.
|
|
@@ -606,7 +738,7 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
606
738
|
* // Set default return value
|
|
607
739
|
* mockFn.mockReturnValue('Default Value');
|
|
608
740
|
*
|
|
609
|
-
* // Set one-time return value for the next call
|
|
741
|
+
* // Set a one-time return value for the next call
|
|
610
742
|
* mockFn.mockReturnValueOnce('First Call');
|
|
611
743
|
* mockFn.mockReturnValueOnce('Second Call');
|
|
612
744
|
*
|
|
@@ -668,7 +800,7 @@ export declare class MockState<ReturnType = unknown, Args extends Array<unknown>
|
|
|
668
800
|
* // Set default rejected value
|
|
669
801
|
* mockFn.mockRejectedValue('Default Error');
|
|
670
802
|
*
|
|
671
|
-
* // Set one-time rejected value for the next call
|
|
803
|
+
* // Set a one-time rejected value for the next call
|
|
672
804
|
* mockFn.mockRejectedValueOnce('First Call Error');
|
|
673
805
|
*
|
|
674
806
|
* mockFn().catch(error => {
|
|
@@ -965,23 +1097,92 @@ declare class ExecutionError extends Error {
|
|
|
965
1097
|
/**
|
|
966
1098
|
* Imports
|
|
967
1099
|
*/
|
|
1100
|
+
declare const proxyRegistry: WeakMap<object, {
|
|
1101
|
+
proxy: unknown;
|
|
1102
|
+
spies: Map<PropertyKey, MockState>;
|
|
1103
|
+
}>;
|
|
968
1104
|
/**
|
|
969
|
-
*
|
|
1105
|
+
* Checks if a property on an object is provided via a proxy mechanism rather than directly defined.
|
|
970
1106
|
*
|
|
971
|
-
* @template
|
|
972
|
-
* @template Key - The key of the property to be mocked.
|
|
1107
|
+
* @template T - The type of object being checked
|
|
973
1108
|
*
|
|
974
|
-
* @param
|
|
975
|
-
* @param key - The property key on the
|
|
976
|
-
* @
|
|
1109
|
+
* @param obj - The object to inspect
|
|
1110
|
+
* @param key - The property key to check on the object
|
|
1111
|
+
* @returns `true` if the property is provided by a proxy, `false` if directly defined
|
|
977
1112
|
*
|
|
978
1113
|
* @remarks
|
|
979
|
-
* This function
|
|
980
|
-
*
|
|
1114
|
+
* This function determines whether a property on an object is being provided through
|
|
1115
|
+
* a proxy mechanism (like a Proxy object or getter) rather than being directly defined
|
|
1116
|
+
* on the object itself. It works by checking if the key doesn't exist in the object's
|
|
1117
|
+
* own properties while still returning a non-undefined value when accessed.
|
|
981
1118
|
*
|
|
982
|
-
*
|
|
1119
|
+
* This is useful for:
|
|
1120
|
+
* - Detecting dynamically created properties
|
|
1121
|
+
* - Identifying properties provided via getters or proxies
|
|
1122
|
+
* - Distinguishing between direct properties and inherited/proxied ones
|
|
1123
|
+
*
|
|
1124
|
+
* @example
|
|
1125
|
+
* ```ts
|
|
1126
|
+
* // Regular object with direct property
|
|
1127
|
+
* const directObj = { name: 'Test' };
|
|
1128
|
+
* console.log(isProxyProperty(directObj, 'name')); // false
|
|
1129
|
+
*
|
|
1130
|
+
* // Object with proxy property
|
|
1131
|
+
* const handler = {
|
|
1132
|
+
* get(target, prop) {
|
|
1133
|
+
* if (prop === 'dynamic') return 'This is dynamic';
|
|
1134
|
+
* return target[prop];
|
|
1135
|
+
* }
|
|
1136
|
+
* };
|
|
1137
|
+
* const proxyObj = new Proxy({}, handler);
|
|
1138
|
+
* console.log(isProxyProperty(proxyObj, 'dynamic')); // true
|
|
1139
|
+
* ```
|
|
1140
|
+
*
|
|
1141
|
+
* @since 1.2.0
|
|
1142
|
+
*/
|
|
1143
|
+
declare function isProxyProperty<T extends object>(obj: T, key: keyof T): boolean;
|
|
1144
|
+
/**
|
|
1145
|
+
* Creates a spy on a property accessed via a proxy getter, allowing interception
|
|
1146
|
+
* and monitoring of property access operations.
|
|
1147
|
+
*
|
|
1148
|
+
* @template T - The type of the target object containing the proxy
|
|
1149
|
+
* @template K - The type of property key being spied on
|
|
1150
|
+
*
|
|
1151
|
+
* @param target - The object containing the property to spy on
|
|
1152
|
+
* @param key - The property key to intercept access to
|
|
1153
|
+
* @param method - The method to execute when the property is accessed
|
|
1154
|
+
* @returns A {@link MockState} instance that tracks interactions with the property
|
|
1155
|
+
*
|
|
1156
|
+
* @throws Error - If the target is not part of any global object
|
|
1157
|
+
*
|
|
1158
|
+
* @example
|
|
1159
|
+
* ```ts
|
|
1160
|
+
* // Create an object with dynamic property access
|
|
1161
|
+
* const user = new Proxy({}, {
|
|
1162
|
+
* get(target, prop) {
|
|
1163
|
+
* if (prop === 'name') return 'John';
|
|
1164
|
+
* return target[prop];
|
|
1165
|
+
* }
|
|
1166
|
+
* });
|
|
1167
|
+
*
|
|
1168
|
+
* // Spy on the 'name' property
|
|
1169
|
+
* const nameSpy = spyOnProxyGet(user, 'name', () => 'Jane');
|
|
1170
|
+
*
|
|
1171
|
+
* // Now accessing user.name returns 'Jane' and the access is tracked
|
|
1172
|
+
* console.log(user.name); // 'Jane'
|
|
1173
|
+
* expect(nameSpy).toHaveBeenCalled();
|
|
1174
|
+
*
|
|
1175
|
+
* // Restore original behavior
|
|
1176
|
+
* nameSpy.mockRestore();
|
|
1177
|
+
* console.log(user.name); // 'John'
|
|
1178
|
+
* ```
|
|
1179
|
+
*
|
|
1180
|
+
* @see MockState
|
|
1181
|
+
* @see getParentObject
|
|
1182
|
+
*
|
|
1183
|
+
* @since 1.2.0
|
|
983
1184
|
*/
|
|
984
|
-
declare function
|
|
1185
|
+
declare function spyOnProxyGet<T extends object, K extends keyof T>(target: T, key: K, method: unknown): MockState;
|
|
985
1186
|
/**
|
|
986
1187
|
* Creates a spy on a specified static method or static property of a target class (not a class instance).
|
|
987
1188
|
* Useful for mocking behavior during testing.
|