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 ref methods to mocked timers #51809

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 29 additions & 1 deletion lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ const TIMERS_DEFAULT_INTERVAL = {
setImmediate: -1,
};

class Timeout {
constructor(opts) {
this.id = opts.id;
this.callback = opts.callback;
this.runAt = opts.runAt;
this.interval = opts.interval;
this.args = opts.args;
}

hasRef() {
return true;
}

ref() {
return this;
}

unref() {
return this;
}

refresh() {
return this;
}
}

class MockTimers {
#realSetTimeout;
#realClearTimeout;
Expand Down Expand Up @@ -260,14 +286,16 @@ class MockTimers {

#createTimer(isInterval, callback, delay, ...args) {
const timerId = this.#currentTimer++;
const timer = {
const opts = {
__proto__: null,
id: timerId,
callback,
runAt: this.#now + delay,
interval: isInterval ? delay : undefined,
args,
};

const timer = new Timeout(opts);
this.#executionQueue.insert(timer);
return timer;
}
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-runner-mock-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,38 @@ describe('Mock Timers Test Suite', () => {
clearTimeout(id);
});
});

describe('Api should have same public properties as original', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding tests that go over all properties so we won't have that problem again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you meanb internal mock properties that are not part of the original Timeout object?

it('should have hasRef', (t) => {
t.mock.timers.enable();
const timer = setTimeout();
assert.strictEqual(typeof timer.hasRef, 'function');
assert.strictEqual(timer.hasRef(), true);
clearTimeout(timer);
});

it('should have ref', (t) => {
t.mock.timers.enable();
const timer = setTimeout();
assert.ok(typeof timer.ref === 'function');
assert.deepStrictEqual(timer.ref(), timer);
clearTimeout(timer);
});

it('should have unref', (t) => {
t.mock.timers.enable();
const timer = setTimeout();
assert.ok(typeof timer.unref === 'function');
assert.deepStrictEqual(timer.unref(), timer);
clearTimeout(timer);
});

it('should have refresh', (t) => {
t.mock.timers.enable();
const timer = setTimeout();
assert.ok(typeof timer.refresh === 'function');
assert.deepStrictEqual(timer.refresh(), timer);
clearTimeout(timer);
});
});
});