Skip to content

Commit

Permalink
test: add Symbol.dispose support to mock timers
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#48549
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Erick Wendel <[email protected]>
  • Loading branch information
sercher committed Apr 25, 2024
1 parent 24a4b63 commit c8c42f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions graal-nodejs/doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,10 @@ const { mock } = require('node:test');
mock.timers.reset();
```

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

Calls `timers.reset()`.

### `timers.tick(milliseconds)`

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

[SymbolDispose]() {
this.reset();
}

reset() {
// Ignore if not enabled
if (!this.#isEnabled) return;
Expand Down
15 changes: 15 additions & 0 deletions graal-nodejs/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

0 comments on commit c8c42f8

Please sign in to comment.