3
votes

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.

1

1 Answers

3
votes

You're not passing the done function into your let's do this spec. Jasmine 2.0 runs the spec as synchronous or asynchronous based on the length property of the spec's function, so a no-args function will always run synchronous.

The code below is from Jasmine's GitHub (/src/core/QueueRunner.js).

for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) {
  var fn = fns[iterativeIndex];
  if (fn.length > 0) {
    return attemptAsync(fn);
  } else {
    attemptSync(fn);
  }
}

Also, don't forget to also call done() in the afterEach function, like so:

afterEach(function(done){
    expect(auth.user.Session).toBe(74790760);
    done();
});