Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: add jsdocs to MockTimers #49476

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/internal/test_runner/mock/mock_timers.js
Expand Up @@ -455,6 +455,13 @@ class MockTimers {
);
}

/**
* Advances the virtual time of MockTimers by the specified duration (in milliseconds).
* This method simulates the passage of time and triggers any scheduled timers that are due.
* @param {number} [time=1] - The amount of time (in milliseconds) to advance the virtual time.
* @throws {ERR_INVALID_STATE} If MockTimers are not enabled.
* @throws {ERR_INVALID_ARG_VALUE} If a negative time value is provided.
*/
tick(time = 1) {
if (!this.#isEnabled) {
throw new ERR_INVALID_STATE(
Expand Down Expand Up @@ -488,6 +495,12 @@ class MockTimers {
}
}

/**
* Enables MockTimers for the specified timers.
* @param {string[]} timers - An array of timer types to enable, e.g., ['setTimeout', 'setInterval'].
* @throws {ERR_INVALID_STATE} If MockTimers are already enabled.
* @throws {ERR_INVALID_ARG_VALUE} If an unsupported timer type is specified.
*/
enable(timers = SUPPORTED_TIMERS) {
if (this.#isEnabled) {
throw new ERR_INVALID_STATE(
Expand All @@ -513,10 +526,17 @@ class MockTimers {
this.#toggleEnableTimers(true);
}

/**
* An alias for `this.reset()`, allowing the disposal of the `MockTimers` instance.
*/
[SymbolDispose]() {
this.reset();
}

/**
* Resets MockTimers, disabling any enabled timers and clearing the execution queue.
* Does nothing if MockTimers are not enabled.
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
*/
reset() {
// Ignore if not enabled
if (!this.#isEnabled) return;
Expand All @@ -531,6 +551,10 @@ class MockTimers {
}
}

/**
* Runs all scheduled timers until there are no more pending timers.
* @throws {ERR_INVALID_STATE} If MockTimers are not enabled.
*/
runAll() {
if (!this.#isEnabled) {
throw new ERR_INVALID_STATE(
Expand Down