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

Cannot clear timer error with sinon/mocha #872

Closed
kennydude opened this issue Oct 8, 2015 · 12 comments
Closed

Cannot clear timer error with sinon/mocha #872

kennydude opened this issue Oct 8, 2015 · 12 comments

Comments

@kennydude
Copy link

I'm dealing with an issue which is cropping up 50% of the time on our tests using Sinon, Mocha and Testem.

It's seemingly random all of the tests will fail starting with:

 Uncaught Error: Cannot clear timer: timer created with setTimeout() but cleared with clearInterval()

And the stack trace (even with source-map-support) doesn't give any clue as to what is causing the issue.

@mantoni
Copy link
Member

mantoni commented Oct 9, 2015

The error message means that you have something like this in your code:

var timer = setTimeout(/*...*/);
// ...
clearInterval(timer);

Detecting this kind of wrong timer usage was added to the fake timer library.
I suggest you're greping through your codebase and replace the clearInterval with a clearTimeout call.

It's unclear to me why this happens only 50% of the time, but failing on this kind of timer usage is intentional and not a bug in Sinon.

@mantoni mantoni closed this as completed Oct 9, 2015
@kennydude
Copy link
Author

No instances of setTimeout or clearInterval exist in the code, it's all handled by underscore :/

@mantoni
Copy link
Member

mantoni commented Oct 9, 2015

Maybe underscore clears timers with clearInterval?

@kennydude
Copy link
Author

Nope :/ There's no mention of clearInterval in the source code

@fatso83
Copy link
Contributor

fatso83 commented Oct 9, 2015

It would help a lot if you could actually make a little snippet of code displaying the issue. This does indeed seem like a bug in client code.

I'd suggest firing up Chrome, setting a breakpoint at the relevant line in sinon, and see where the stack trace leads you.

Re the relevant line, I'd checkout the vicinity of line 971:

$ ack -C10 'Cannot clear timer'
pkg/sinon-1.17.0.js
961-        if (typeof timerId === "object") {
962-            timerId = timerId.id;
963-        }
964-
965-        if (clock.timers.hasOwnProperty(timerId)) {
966-            // check that the ID matches a timer of the correct type
967-            var timer = clock.timers[timerId];
968-            if (timerType(timer) === ttype) {
969-                delete clock.timers[timerId];
970-            } else {
971:                throw new Error("Cannot clear timer: timer created with set" + ttype + "() but cleared with clear" + timerType(timer) + "()");
972-            }

@kennydude
Copy link
Author

The funny thing is this only ever shows up on CircleCI for me so I can't do that. I did a search for clearTimeout and it could be Bootstrap causing issues.

So I might have cleared it by disabling it, hopefully.

@pabigot
Copy link

pabigot commented Nov 5, 2017

I've also encountered this and finally tracked it down: if clearInterval is called with an undefined interval sinon appears to record it as a timer rather than an interval.

Reproducible with mocha 4.0.1 and sinon 4.1.1 and this code under Node.js 4.5:

'use strict';

const sinon = require('sinon');

describe('Test', function() {
  let clock;
  beforeEach(() => {
    clock = sinon.useFakeTimers(24 * 60 * 60 * 1000);
  });
  afterEach(() => clock.restore());
  it('should work', function() {
    const id = setInterval(() => null);
    clearInterval(id);
  });
});

producing:

 lilith[49]$ node_modules/.bin/mocha sinon872.js 


  Test
    1) should work


  0 passing (9ms)
  1 failing

  1) Test
       should work:
     Error: Cannot clear timer: timer created with setTimeout() but cleared with clearInterval()
      at clearTimer (node_modules/sinon/node_modules/lolex/src/lolex-src.js:359:19)
      at Object.clearInterval (node_modules/sinon/node_modules/lolex/src/lolex-src.js:504:16)
      at target.(anonymous function) (node_modules/sinon/node_modules/lolex/src/lolex-src.js:406:34)
      at Context.<anonymous> (sinon872.js:13:5)

@mroderick
Copy link
Member

@pabigot thank you for the test case ❤️

Would you consider making a PR to fix it also?

@mroderick mroderick reopened this Nov 6, 2017
@fatso83
Copy link
Contributor

fatso83 commented Nov 6, 2017

@mroderick, @pabigot created #1611, which I closed after moving it to sinonjs/fake-timers#138. FYI

@pabigot
Copy link

pabigot commented Nov 6, 2017

Fix submitted as sinonjs/fake-timers#139.

fatso83 added a commit to fatso83/sinon that referenced this issue Nov 7, 2017
@fatso83 fatso83 closed this as completed in 8de60ac Nov 7, 2017
@mroderick
Copy link
Member

@pabigot Thank you 👍

@fatso83
Copy link
Contributor

fatso83 commented Nov 7, 2017

Fix released as 4.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants