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

jasmine.Clock broken in node.js #171

Closed
lsegal opened this issue Sep 13, 2012 · 15 comments
Closed

jasmine.Clock broken in node.js #171

lsegal opened this issue Sep 13, 2012 · 15 comments
Labels
Milestone

Comments

@lsegal
Copy link

lsegal commented Sep 13, 2012

Seems related to this google groups thread

Running this example from pivotal.github.com/jasmine fails:

describe("Manually ticking the Jasmine Mock Clock", function() {
  var timerCallback;

  beforeEach(function() {
    timerCallback = jasmine.createSpy('timerCallback');
    jasmine.Clock.useMock();
  });

  it("causes a timeout to be called synchronously", function() {
    setTimeout(function() {
      timerCallback();
    }, 100);

    expect(timerCallback).not.toHaveBeenCalled();

    jasmine.Clock.tick(101);

    expect(timerCallback).toHaveBeenCalled();
  });

  it("causes an interval to be called synchronously", function() {
    setInterval(function() {
      timerCallback();
    }, 100);

    expect(timerCallback).not.toHaveBeenCalled();

    jasmine.Clock.tick(101);
    expect(timerCallback.callCount).toEqual(1);

    jasmine.Clock.tick(50);
    expect(timerCallback.callCount).toEqual(1);

    jasmine.Clock.tick(50);
    expect(timerCallback.callCount).toEqual(2);
  });
});
@V1c70r
Copy link

V1c70r commented Sep 30, 2012

I have the same problem.

@nfriedly
Copy link
Contributor

nfriedly commented Oct 5, 2012

I'm hitting this too. runs() and waitFor() work, but in the end I just tweaked my JS to use a utility method that calls setTimeout() and then had jasmine spy on that utility and call a fake that just fires the callback immediately.

@lsegal
Copy link
Author

lsegal commented Oct 5, 2012

Indeed, I worked around this by re-assigning the setTimeout global to a mock that increments a global counter for "timeWaited". We can assert the time delays by checking this variable. Looks something like:

var oldSetTimeout, timeWaited;
beforeEach(function() {
  oldSetTimeout = setTimeout;
  timeWaited = 0;
  setTimeout = function(callback, timeout) {
    timeWaited += timeout;
    callback(); // FIXME handle callback passed as String
  };
});

afterEach(function() { setTimeout = oldSetTimeout; });

@pathsny
Copy link

pathsny commented Dec 10, 2012

this is in my spec helper. it solves the problem jasmine.getGlobal = function() { return GLOBAL; }

jasmine.getGlobal().setTimeout = function(funcToCall, millis) {
  if (jasmine.Clock.installed.setTimeout.apply) {
    return jasmine.Clock.installed.setTimeout.apply(this, arguments);
  } else {
    return jasmine.Clock.installed.setTimeout(funcToCall, millis);
  }
};

and so on. All 4 lines from jasmine.js

@mainiak
Copy link

mainiak commented Jan 3, 2013

Temporarily solved with SinonJS fake timers which could be used separately.

@tebriel
Copy link
Contributor

tebriel commented Feb 2, 2013

I've got this fixed in a new beta, I'll push it up here after 1.1.1 is released.

@ghost ghost assigned tebriel Feb 2, 2013
tebriel pushed a commit that referenced this issue Feb 2, 2013
@tebriel
Copy link
Contributor

tebriel commented Feb 2, 2013

Ready for publish. Currently in 1.2.0 branch.

@tebriel tebriel closed this as completed Feb 2, 2013
@nfriedly
Copy link
Contributor

nfriedly commented Feb 3, 2013

Whoot! Drinks on me if we ever meetup somewhere :)

@tebriel
Copy link
Contributor

tebriel commented Feb 3, 2013

Hah. I'll hold you to that! Have you verified? It works in my tests,knowing it works somewhere else would be awesome.

@nfriedly
Copy link
Contributor

nfriedly commented Feb 3, 2013

No, but I'll try and get it in early this week and let you know.

@tebriel
Copy link
Contributor

tebriel commented Feb 3, 2013

Awesome, thanks!

@nfriedly
Copy link
Contributor

nfriedly commented Feb 7, 2013

Just updated and it works perfectly! I removed all the runs() and waitsFor()s from my code and now all of my tests combined finish in just under 2 seconds!

@tebriel
Copy link
Contributor

tebriel commented Feb 7, 2013

Fantastic! Glad to hear it.

@mainiak
Copy link

mainiak commented Apr 17, 2013

Thank you for the fix - but I am still having issues. See https://gist.github.com/mainiak/4355050

Line below will fail. Seems like clearInterval isn't working.

git clone https://gist.github.com/4355050.git && cd 4355050 && patch -i nodejs.patch && jasmine-node --coffee --verbose .

@vvondra
Copy link

vvondra commented Jun 23, 2015

I am still having the issue above form @mainiak

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

No branches or pull requests

7 participants