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

Add countTimers method #215

Merged
merged 4 commits into from
Oct 8, 2018
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
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ available in both browser & node environments.

Cancels the callback scheduled by the provided id.

### `clock.countTimers()`

Returns the number of waiting timers. This can be used to assert that a test
finishes without leaking any timers.

### `clock.hrtime(prevTime?)`
Only available in Node.js, mimicks process.hrtime().

Expand Down
5 changes: 4 additions & 1 deletion src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ function withGlobal(_global) {
return timer.id;
}


/* eslint consistent-return: "off" */
function compareTimers(a, b) {
// Sort first by absolute timing
Expand Down Expand Up @@ -574,6 +573,10 @@ function withGlobal(_global) {
return clearTimer(clock, timerId, "Immediate");
};

clock.countTimers = function countTimers() {
return Object.keys(clock.timers || {}).length;
};

clock.requestAnimationFrame = function requestAnimationFrame(func) {
var result = addTimer(clock, {
func: func,
Expand Down
20 changes: 20 additions & 0 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,26 @@ describe("lolex", function () {

});

describe("countTimers", function () {

beforeEach(function () {
this.clock = lolex.createClock();
});

it("return zero for a fresh clock", function () {
assert.equals(this.clock.countTimers(), 0);
});

it("counts remaining timers", function () {
this.clock.setTimeout(NOOP, 100);
this.clock.setTimeout(NOOP, 200);
this.clock.setTimeout(NOOP, 300);
this.clock.tick(150);
assert.equals(this.clock.countTimers(), 2);
});

});

describe("tick", function () {

beforeEach(function () {
Expand Down