I recently started migrating from jasmine 1.3 to 2.0 and ran into some problems.
This is how my old test looked like:
it("should start heartbeat after successful login and stop heartbeat after logout", function () {
runs(function () {
auth.hbTimeout = 500;
var loggedIn = auth.login("USERWITHSESSION", "xyz", {});
expect(loggedIn).toBe(true);
expect(auth.getAuthenticated()).toBe(true);
expect(auth.user).toBeDefined();
expect(auth.user.Session).toEqual(74790750);
setTimeout(function () {
auth.stopHeartbeat();
auth.user.Session = 74790760;
}, 2000);
});
waitsFor(function () {
return auth.user.Session == 74790760;
}, "The session-id should have been changed", 2600);
runs(function () {
auth.heartbeat();
expect(auth.getAuthenticated()).toBe(false);
expect(auth.user).not.toBeDefined();
auth.login("USERWITHSESSION", "xyz", {});
setTimeout(function () {
auth.user.Session = 74790750;
}, 500);
});
waitsFor(function () {
return auth.user.Session == 74790750;
}, "The session-id should have been changed back", 1100);
runs(function () {
setTimeout(function () {
auth.logout();
}, 2000);
});
waitsFor(function () {
return auth.getAuthenticated() == false;
});
expect(auth.user).not.toBeDefined();
});
I want to replicate the part up until the first waitsFor(). for the two second timeout I tried a setTimout() and moved the expect into an afterEach.
As far as I understand it, jasmine should wait two seconds an then execute the code, still the expect is always wrong and the test fails.
This is how I did it:
describe("this is a async nested describe",function(){
afterEach(function(done){
expect(auth.user.Session).toBe(74790760);
});
it("let's do this",function(){
auth.hbTimeout = 500;
var loggedIn = auth.login("USERWITHSESSION", "xyz", {});
expect(loggedIn).toBe(true);
expect(auth.getAuthenticated()).toBe(true);
expect(auth.user).toBeDefined();
expect(auth.user.Session).toEqual(74790750);
setTimeout(function() {
auth.stopHeartbeat();
auth.user.Session = 74790760;
done();
},2000);
});
});
Can someone give me a hint? Whatever I do, even if I set the timeout to a minute, the test still reaches the expect in the same amount of time.