Question

In QUnit you can specify how many assertions are expected to run within a test
i.e. expect(2)

Is there way to do this in Jasmine 2.0? Though their docs are clear, I can't seem to find an exhaustive API list anywhere.

I would like to run multiple async tests without having to nest them. For example, the third test should reliably be last. But if the first two fail completely, the light is still green because done() is called.

it("Should run callbacks with correct data", function(done){

    //expect(3); //QUnit syntax

    test.fastResponse(function(data){
        expect(data).toEqual({a:1, b:2});
    });

    test.fastResponse(function(data){
        expect(data).toEqual({a:1, b:2});
    });

    test.slowResponse(function(data){
        expect(data).toEqual({a:1, b:2});
        //This should fail if the other two tests didn't run
        done();
    });

});
Was it helpful?

Solution

I would recommend adding a callCount variable, and incrementing it by 1 each time a callback is invoked. Then you can expect(callCount).toBe(x) just before the call to done().

On the other hand, you can use Spies to achieve this as well:

it("Should run callbacks with correct data", function(done){
    var callback = jasmine.createSpy("callback");

    var actuallyDone = function() {
        expect(callback.calls.count()).toBe(2);
        expect(callback.calls.all()[0].args[0]).toEqual({a:1, b:2});
        expect(callback.calls.all()[1].args[0]).toEqual({a:1, b:2});
        done();
    };

    test.fastResponse(callback);
    test.fastResponse(callback);

    test.slowResponse(function(data){
        expect(data).toEqual({a:1, b:2});
        actuallyDone();
    });

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top