Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine chaining tests

I am using Jasmine to test my project. I use it for integration testing, not for unit testing.

describe("admin", function () {

    var testUser =  {
        name: 'test',
        pass: 'pass'
    };

    it("should be able to create a new user", function(done) {
        needle.post(server + "addUser.php", testUser, function (err, res) {
            expect(res.statusCode).toBe(200);
            done();
        });  
    });

    it("should be able to delete the user", function(done) {
        needle.post(server + "deletedUser.php", testUser, function (err, res) {
            expect(res.statusCode).toBe(200);
            done();
        });  
    });


});

How can I make sure the delete test runs after the add test?

like image 996
XCS Avatar asked Dec 26 '22 02:12

XCS


2 Answers

Ok, the answer may be this simple: Jasmine runs tests in the order of their appearance, even the async ones.

To test this I tried with:

it("A", function (done) {
    console.log("A");
    setTimeout(done, 4000); 
});

it("B", function (done) {
    console.log("B");
    setTimeout(done, 20);
});

it("C", function () { 
    console.log("C");
});

And the output was ABC, test B doesn't start before done from test A is called.

like image 35
XCS Avatar answered Jan 04 '23 17:01

XCS


For anyone who might stumble across this: using a slightly larger test set shows that order is not guaranteed (since jasmine 2.3.0):

it('a', function (done) { console.log('a'); setTimeout(done, 100); });
it('b', function (done) { console.log('b'); setTimeout(done, 100); });
it('c', function (done) { console.log('c'); setTimeout(done, 100); });
it('d', function (done) { console.log('d'); setTimeout(done, 100); });
it('e', function (done) { console.log('e'); setTimeout(done, 100); });
it('f', function (done) { console.log('f'); setTimeout(done, 100); });
it('g', function (done) { console.log('g'); setTimeout(done, 100); });
it('h', function (done) { console.log('h'); setTimeout(done, 100); });
it('i', function (done) { console.log('i'); setTimeout(done, 100); });
it('j', function (done) { console.log('j'); setTimeout(done, 100); });
it('k', function (done) { console.log('k'); setTimeout(done, 100); });
it('l', function (done) { console.log('l'); setTimeout(done, 100); });
it('m', function (done) { console.log('m'); setTimeout(done, 100); });
it('n', function (done) { console.log('n'); setTimeout(done, 100); });
it('o', function (done) { console.log('o'); setTimeout(done, 100); });
it('p', function (done) { console.log('p'); setTimeout(done, 100); });
it('q', function (done) { console.log('q'); setTimeout(done, 100); });
it('r', function (done) { console.log('r'); setTimeout(done, 100); });
it('s', function (done) { console.log('s'); setTimeout(done, 100); });
it('t', function (done) { console.log('t'); setTimeout(done, 100); });
it('u', function (done) { console.log('u'); setTimeout(done, 100); });
it('v', function (done) { console.log('v'); setTimeout(done, 100); });
it('w', function (done) { console.log('w'); setTimeout(done, 100); });
it('x', function (done) { console.log('x'); setTimeout(done, 100); });
it('y', function (done) { console.log('y'); setTimeout(done, 100); });
it('z', function (done) { console.log('z'); setTimeout(done, 100); });

// 2.1.0: a b c d e f g h i l k l m n o p q r s t u v w x y z
// 2.2.0: a b c d e f g h i l k l m n o p q r s t u v w x y z
// 2.3.0: a z c d e f g h i j k l m b o p q r s t u v w x y n
// 2.3.1: a z c d e f g h i j k l m b o p q r s t u v w x y n

Follow along here for updates (if any).

UPDATE: it was a bug and was fixed with v2.3.4. All tests should be ordered now.

like image 133
Jason Cust Avatar answered Jan 04 '23 17:01

Jason Cust