Skip to content

Commit

Permalink
Merge pull request #215 from thejohnfreeman/master
Browse files Browse the repository at this point in the history
Add countTimers method
  • Loading branch information
fatso83 authored Oct 8, 2018
2 parents 120d39e + 16769a1 commit 00f3a5f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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 @@ -259,7 +259,6 @@ function withGlobal(_global) {
return timer.id;
}


/* eslint consistent-return: "off" */
function compareTimers(a, b) {
// Sort first by absolute timing
Expand Down Expand Up @@ -601,6 +600,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 @@ -519,6 +519,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

0 comments on commit 00f3a5f

Please sign in to comment.