Skip to content

Commit 2769f7c

Browse files
committed
test_runner: add jsdocs to mock.js
1 parent 3163f8d commit 2769f7c

File tree

1 file changed

+85
-0
lines changed
  • lib/internal/test_runner/mock

1 file changed

+85
-0
lines changed

lib/internal/test_runner/mock/mock.js

+85
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,36 @@ class MockFunctionContext {
4545
this.#times = times;
4646
}
4747

48+
/**
49+
* Gets an array of recorded calls made to the mock function.
50+
* @returns {Array} An array of recorded calls.
51+
*/
4852
get calls() {
4953
return ArrayPrototypeSlice(this.#calls, 0);
5054
}
5155

56+
/**
57+
* Retrieves the number of times the mock function has been called.
58+
* @returns {number} The call count.
59+
*/
5260
callCount() {
5361
return this.#calls.length;
5462
}
5563

64+
/**
65+
* Sets a new implementation for the mock function.
66+
* @param {Function} implementation - The new implementation for the mock function.
67+
*/
5668
mockImplementation(implementation) {
5769
validateFunction(implementation, 'implementation');
5870
this.#implementation = implementation;
5971
}
6072

73+
/**
74+
* Replaces the implementation of the function only once.
75+
* @param {Function} implementation - The substitute function.
76+
* @param {number} [onCall] - The call index to be replaced.
77+
*/
6178
mockImplementationOnce(implementation, onCall) {
6279
validateFunction(implementation, 'implementation');
6380
const nextCall = this.#calls.length;
@@ -66,6 +83,9 @@ class MockFunctionContext {
6683
this.#mocks.set(call, implementation);
6784
}
6885

86+
/**
87+
* Restores the original function that was mocked.
88+
*/
6989
restore() {
7090
const { descriptor, object, original, methodName } = this.#restore;
7191

@@ -79,14 +99,25 @@ class MockFunctionContext {
7999
}
80100
}
81101

102+
/**
103+
* Resets the recorded calls to the mock function
104+
*/
82105
resetCalls() {
83106
this.#calls = [];
84107
}
85108

109+
/**
110+
* Tracks a call made to the mock function.
111+
* @param {object} call - The call details.
112+
*/
86113
trackCall(call) {
87114
ArrayPrototypePush(this.#calls, call);
88115
}
89116

117+
/**
118+
* Gets the next implementation to use for the mock function.
119+
* @returns {Function} The next implementation.
120+
*/
90121
nextImpl() {
91122
const nextCall = this.#calls.length;
92123
const mock = this.#mocks.get(nextCall);
@@ -109,11 +140,24 @@ class MockTracker {
109140
#mocks = [];
110141
#timers;
111142

143+
/**
144+
* .
145+
* Returns the mock timers of this MockTracker instance.
146+
* @returns {MockTimers} The mock timers instance.
147+
*/
112148
get timers() {
113149
this.#timers ??= new MockTimers();
114150
return this.#timers;
115151
}
116152

153+
/**
154+
* Creates a mock function tracker.
155+
* @param {Function} [original] - The original function to be tracked.
156+
* @param {Function} [implementation] - An optional replacement function for the original one.
157+
* @param {object} [options] - Additional tracking options.
158+
* @param {number} [options.times=Infinity] - The maximum number of times the mock function can be called.
159+
* @returns {ProxyConstructor} The mock function tracker.
160+
*/
117161
fn(
118162
original = function() {},
119163
implementation = original,
@@ -137,6 +181,17 @@ class MockTracker {
137181
return this.#setupMock(ctx, original);
138182
}
139183

184+
/**
185+
* Creates a method tracker for a specified object or function.
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.
194+
*/
140195
method(
141196
objectOrFunction,
142197
methodName,
@@ -216,6 +271,18 @@ class MockTracker {
216271
return mock;
217272
}
218273

274+
/**
275+
* Mocks a getter method of an object.
276+
* This is a syntax sugar for the MockTracker.method with options.getter set to true
277+
* @param {object} object - The target object.
278+
* @param {string} methodName - The name of the getter method to be mocked.
279+
* @param {Function} [implementation] - An optional replacement function for the targeted method.
280+
* @param {object} [options] - Additional tracking options.
281+
* @param {boolean} [options.getter=true] - Indicates whether this is a getter method.
282+
* @param {boolean} [options.setter=false] - Indicates whether this is a setter method.
283+
* @param {number} [options.times=Infinity] - The maximum number of times the mock method can be called.
284+
* @returns {ProxyConstructor} The mock method tracker.
285+
*/
219286
getter(
220287
object,
221288
methodName,
@@ -244,6 +311,18 @@ class MockTracker {
244311
});
245312
}
246313

314+
/**
315+
* Mocks a setter method of an object.
316+
* This function is a syntax sugar for MockTracker.method with options.setter set to true.
317+
* @param {object} object - The target object.
318+
* @param {string} methodName - The setter method to be mocked.
319+
* @param {Function} [implementation] - An optional replacement function for the targeted method.
320+
* @param {object} [options] - Additional tracking options.
321+
* @param {boolean} [options.getter=false] - Indicates whether this is a getter method.
322+
* @param {boolean} [options.setter=true] - Indicates whether this is a setter method.
323+
* @param {number} [options.times=Infinity] - The maximum number of times the mock method can be called.
324+
* @returns {ProxyConstructor} The mock method tracker.
325+
*/
247326
setter(
248327
object,
249328
methodName,
@@ -272,12 +351,18 @@ class MockTracker {
272351
});
273352
}
274353

354+
/**
355+
* Resets the mock tracker, restoring all mocks and clearing timers.
356+
*/
275357
reset() {
276358
this.restoreAll();
277359
this.#timers?.reset();
278360
this.#mocks = [];
279361
}
280362

363+
/**
364+
* Restore all mocks created by this MockTracker instance.
365+
*/
281366
restoreAll() {
282367
for (let i = 0; i < this.#mocks.length; i++) {
283368
FunctionPrototypeCall(restore, this.#mocks[i]);

0 commit comments

Comments
 (0)