@@ -28,7 +28,7 @@ const {
28
28
} = require ( 'internal/validators' ) ;
29
29
const { MockTimers } = require ( 'internal/test_runner/mock/mock_timers' ) ;
30
30
31
- function kDefaultFunction ( ) { }
31
+ function kDefaultFunction ( ) { }
32
32
33
33
class MockFunctionContext {
34
34
#calls;
@@ -46,24 +46,24 @@ class MockFunctionContext {
46
46
}
47
47
48
48
/**
49
- * Returns an array of all function calls.
50
- * @returns {Array } An array containing all function calls.
49
+ * Gets an array of recorded calls made to the mock function .
50
+ * @returns {Array } An array of recorded calls.
51
51
*/
52
52
get calls ( ) {
53
53
return ArrayPrototypeSlice ( this . #calls, 0 ) ;
54
54
}
55
55
56
56
/**
57
- * Retrieves the number of calls to the mock function.
58
- * @returns {number } The number of calls .
57
+ * Retrieves the number of times the mock function has been called .
58
+ * @returns {number } The call count .
59
59
*/
60
60
callCount ( ) {
61
61
return this . #calls. length ;
62
62
}
63
63
64
64
/**
65
- * Replaces the implementation of the function.
66
- * @param {Function } implementation - The substitute function.
65
+ * Sets a new implementation for the mock function.
66
+ * @param {function } implementation - The new implementation for the mock function.
67
67
*/
68
68
mockImplementation ( implementation ) {
69
69
validateFunction ( implementation , 'implementation' ) ;
@@ -72,7 +72,7 @@ class MockFunctionContext {
72
72
73
73
/**
74
74
* Replaces the implementation of the function only once.
75
- * @param {Function } implementation - The substitute function.
75
+ * @param {function } implementation - The substitute function.
76
76
* @param {number } [onCall] - The call index to be replaced.
77
77
*/
78
78
mockImplementationOnce ( implementation , onCall ) {
@@ -84,7 +84,7 @@ class MockFunctionContext {
84
84
}
85
85
86
86
/**
87
- * Discards all implementation mock changes .
87
+ * Restores the original function that was mocked .
88
88
*/
89
89
restore ( ) {
90
90
const { descriptor, object, original, methodName } = this . #restore;
@@ -100,23 +100,23 @@ class MockFunctionContext {
100
100
}
101
101
102
102
/**
103
- * Resets all function calls information.
103
+ * Resets the recorded calls to the mock function
104
104
*/
105
105
resetCalls ( ) {
106
106
this . #calls = [ ] ;
107
107
}
108
108
109
109
/**
110
- * Tracks the mock function call .
110
+ * Tracks a call made to the mock function.
111
111
* @param {Object } call - The call details.
112
112
*/
113
113
trackCall ( call ) {
114
114
ArrayPrototypePush ( this . #calls, call ) ;
115
115
}
116
116
117
117
/**
118
- * Returns the next function implementation for the mock.
119
- * @returns {Function } The next function implementation.
118
+ * Gets the next implementation to use for the mock function .
119
+ * @returns {Function } The next implementation.
120
120
*/
121
121
nextImpl ( ) {
122
122
const nextCall = this . #calls. length ;
@@ -140,20 +140,25 @@ class MockTracker {
140
140
#mocks = [ ] ;
141
141
#timers;
142
142
143
+ /**
144
+ * Returns the mock timers of this MockTracker instance.
145
+ * @returns {MockTimers } The mock timers instance.
146
+ */
143
147
get timers ( ) {
144
148
this . #timers ??= new MockTimers ( ) ;
145
149
return this . #timers;
146
150
}
147
151
148
152
/**
149
- * Creates a mock tracker of a function.
150
- * @param {Function } [original] - The original function to be tracked.
151
- * @param {Function } [implementation] - An optional function to replace the original function being tracked.
152
- * @param {Object } [options]
153
- * @returns {ProxyConstructor } The mock function.
153
+ * Creates a mock function tracker.
154
+ * @param {function } [original] - The original function to be tracked.
155
+ * @param {function } [implementation] - An optional replacement function for the original one.
156
+ * @param {Object } [options] - Additional tracking options.
157
+ * @param {number } [options.times=Infinity] - The maximum number of times the mock function can be called.
158
+ * @returns {ProxyConstructor } The mock function tracker.
154
159
*/
155
160
fn (
156
- original = function ( ) { } ,
161
+ original = function ( ) { } ,
157
162
implementation = original ,
158
163
options = kEmptyObject ,
159
164
) {
@@ -175,13 +180,17 @@ class MockTracker {
175
180
return this . #setupMock( ctx , original ) ;
176
181
}
177
182
178
- /**
179
- * Creates a MockTracker on a specified method, that's part of an object or function.
180
- * @param {(Object | Function) } objectOrFunction
181
- * @param {string } methodName
182
- * @param {Function } [implementation] - An optional function to replace the original method being tracked.
183
- * @param {* } options
184
- * @returns
183
+ /**
184
+ * Creates a method tracker for a specified object or function.
185
+ *
186
+ * @param {(Object | Function) } objectOrFunction - The object or function containing the method to be tracked.
187
+ * @param {string } methodName - The name of the method to be tracked.
188
+ * @param {function } [implementation] - An optional replacement function for the original method.
189
+ * @param {Object } [options] - Additional tracking options.
190
+ * @param {boolean } [options.getter=false] - Indicates whether this is a getter method.
191
+ * @param {boolean } [options.setter=false] - Indicates whether this is a setter method.
192
+ * @param {number } [options.times=Infinity] - The maximum number of times the mock method can be called.
193
+ * @returns {ProxyConstructor } The mock method tracker.
185
194
*/
186
195
method (
187
196
objectOrFunction ,
@@ -262,6 +271,14 @@ class MockTracker {
262
271
return mock ;
263
272
}
264
273
274
+ /**
275
+ * Mocks a getter method of an object.
276
+ * @param {Object } object - The target object.
277
+ * @param {string } methodName - The name of the getter method to be mocked.
278
+ * @param {function } [implementation] - An optional replacement function for the targeted method.
279
+ * @param {Object } [options] - Additional tracking options.
280
+ * @returns {ProxyConstructor } The mock method tracker.
281
+ */
265
282
getter (
266
283
object ,
267
284
methodName ,
@@ -290,6 +307,14 @@ class MockTracker {
290
307
} ) ;
291
308
}
292
309
310
+ /**
311
+ * Mocks a setter method of an object.
312
+ * @param {Object } object - The target object.
313
+ * @param {string } methodName - The setter method to be mocked.
314
+ * @param {function } [implementation] - An optional replacement function for the targeted method.
315
+ * @param {Object } [options] - Additional tracking options.
316
+ * @returns {ProxyConstructor } The mock method tracker.
317
+ */
293
318
setter (
294
319
object ,
295
320
methodName ,
@@ -319,7 +344,7 @@ class MockTracker {
319
344
}
320
345
321
346
/**
322
- * Resets the mock tracker.
347
+ * Resets the mock tracker, restoring all mocks and clearing timers .
323
348
*/
324
349
reset ( ) {
325
350
this . restoreAll ( ) ;
@@ -328,7 +353,7 @@ class MockTracker {
328
353
}
329
354
330
355
/**
331
- * Restore all mocks of this MockTracker.
356
+ * Restore all mocks created by this MockTracker instance .
332
357
*/
333
358
restoreAll ( ) {
334
359
for ( let i = 0 ; i < this . #mocks. length ; i ++ ) {
0 commit comments