Skip to content

Commit cae4aa5

Browse files
committed
Fix PR comments and add jsdocs to getters, setters and get timers()
1 parent b9e7aae commit cae4aa5

File tree

1 file changed

+53
-28
lines changed
  • lib/internal/test_runner/mock

1 file changed

+53
-28
lines changed

lib/internal/test_runner/mock/mock.js

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const {
2828
} = require('internal/validators');
2929
const { MockTimers } = require('internal/test_runner/mock/mock_timers');
3030

31-
function kDefaultFunction() { }
31+
function kDefaultFunction() {}
3232

3333
class MockFunctionContext {
3434
#calls;
@@ -46,24 +46,24 @@ class MockFunctionContext {
4646
}
4747

4848
/**
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.
5151
*/
5252
get calls() {
5353
return ArrayPrototypeSlice(this.#calls, 0);
5454
}
5555

5656
/**
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.
5959
*/
6060
callCount() {
6161
return this.#calls.length;
6262
}
6363

6464
/**
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.
6767
*/
6868
mockImplementation(implementation) {
6969
validateFunction(implementation, 'implementation');
@@ -72,7 +72,7 @@ class MockFunctionContext {
7272

7373
/**
7474
* Replaces the implementation of the function only once.
75-
* @param {Function} implementation - The substitute function.
75+
* @param {function} implementation - The substitute function.
7676
* @param {number} [onCall] - The call index to be replaced.
7777
*/
7878
mockImplementationOnce(implementation, onCall) {
@@ -84,7 +84,7 @@ class MockFunctionContext {
8484
}
8585

8686
/**
87-
* Discards all implementation mock changes.
87+
* Restores the original function that was mocked.
8888
*/
8989
restore() {
9090
const { descriptor, object, original, methodName } = this.#restore;
@@ -100,23 +100,23 @@ class MockFunctionContext {
100100
}
101101

102102
/**
103-
* Resets all function calls information.
103+
* Resets the recorded calls to the mock function
104104
*/
105105
resetCalls() {
106106
this.#calls = [];
107107
}
108108

109109
/**
110-
* Tracks the mock function call.
110+
* Tracks a call made to the mock function.
111111
* @param {Object} call - The call details.
112112
*/
113113
trackCall(call) {
114114
ArrayPrototypePush(this.#calls, call);
115115
}
116116

117117
/**
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.
120120
*/
121121
nextImpl() {
122122
const nextCall = this.#calls.length;
@@ -140,20 +140,25 @@ class MockTracker {
140140
#mocks = [];
141141
#timers;
142142

143+
/**
144+
* Returns the mock timers of this MockTracker instance.
145+
* @returns {MockTimers} The mock timers instance.
146+
*/
143147
get timers() {
144148
this.#timers ??= new MockTimers();
145149
return this.#timers;
146150
}
147151

148152
/**
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.
154159
*/
155160
fn(
156-
original = function () { },
161+
original = function () {},
157162
implementation = original,
158163
options = kEmptyObject,
159164
) {
@@ -175,13 +180,17 @@ class MockTracker {
175180
return this.#setupMock(ctx, original);
176181
}
177182

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.
185194
*/
186195
method(
187196
objectOrFunction,
@@ -262,6 +271,14 @@ class MockTracker {
262271
return mock;
263272
}
264273

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+
*/
265282
getter(
266283
object,
267284
methodName,
@@ -290,6 +307,14 @@ class MockTracker {
290307
});
291308
}
292309

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+
*/
293318
setter(
294319
object,
295320
methodName,
@@ -319,7 +344,7 @@ class MockTracker {
319344
}
320345

321346
/**
322-
* Resets the mock tracker.
347+
* Resets the mock tracker, restoring all mocks and clearing timers.
323348
*/
324349
reset() {
325350
this.restoreAll();
@@ -328,7 +353,7 @@ class MockTracker {
328353
}
329354

330355
/**
331-
* Restore all mocks of this MockTracker.
356+
* Restore all mocks created by this MockTracker instance.
332357
*/
333358
restoreAll() {
334359
for (let i = 0; i < this.#mocks.length; i++) {

0 commit comments

Comments
 (0)