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: add Symbol.dispose support to mocktimers #48549

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,10 @@ const { mock } = require('node:test');
mock.timers.reset();
```

### `timers[Symbol.dispose]()`

An alias for `timers.reset()`.
benjamingr marked this conversation as resolved.
Show resolved Hide resolved

### `timers.tick(milliseconds)`

<!-- YAML
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
FunctionPrototypeBind,
Promise,
SymbolAsyncIterator,
SymbolDispose,
globalThis,
} = primordials;
const {
Expand Down Expand Up @@ -316,6 +317,10 @@ class MockTimers {
this.#toggleEnableTimers(true);
}

[SymbolDispose]() {
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
this.reset();
}

reset() {
// Ignore if not enabled
if (!this.#isEnabled) return;
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-runner-mock-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ describe('Mock Timers Test Suite', () => {
assert.strictEqual(fn.mock.callCount(), 0);
});

it('should reset all timers when calling Symbol.dispose', (t) => {
t.mock.timers.enable();
const fn = t.mock.fn();
global.setTimeout(fn, 1000);
// TODO(benjamingr) refactor to `using`
t.mock.timers[Symbol.dispose]();
assert.throws(() => {
t.mock.timers.tick(1000);
}, {
code: 'ERR_INVALID_STATE',
});

assert.strictEqual(fn.mock.callCount(), 0);
});

it('should execute in order if timeout is the same', (t) => {
t.mock.timers.enable();
const order = [];
Expand Down