Search code examples
node.jschakram

Chakram test not working


I was looking for a new framework and I came across, with chakram, now I trying to do the following test:

Make an api call (get) and this returns an array of elements which I need to iterate and use it's Id to make another api call and then assert the contents. Below an example of the array.

[
    {
        "id": "1",
        "user": "user",     
    },
    {
        "id": "2",
        "user": "user",     
    },
    {
        "id": "3",
        "user": "user",     
    },
    {
        "id": "4",
        "user": "user",     
    },
    {
        "id": "5",
        "user": "user",     
    }
]

What i'm unable is to make another api call using each of the id''s in the response.

Here is my test:

describe("Call registered user", function(){
    it("Validate all user data is ok", function(){
      this.timeout(25000)
        return chakram.get(config.environment.url)
            .then(function(response){
                //console.log(JSON.stringify(response,null, 4));
                for(var i=0; i < response.length; i++ ){
                  console.log(config.environment.url+"/"+response.data[i].id);
                    return chakram.get(config.environment.url+"/"+response.data[i].id)
                        .then(function(userData){
                          console.log(i);
                          expect(userData.response.statusCode).to.equal(200)
                          return chakram.wait();
                        });
                }
            })
    });
});

The problems is that the test doesn't reach the for. Could anyone point me out what I'm doing wrong. Btw I'm a noob at JS.


Solution

  • First of all I would like to point out that .then must come one after the other.

    And you can use a it("", () => {}) for each test.

    So,

    let testArray;
    
    function testFunction() {
        return chakram.get(config.environment.url)
            .then(response => {
                testArray = response;
            })
    }
    
    describe("Call registered user", function(){
        testFunction();
    
        testArray.map( user => {
            it("Validate userId: " + user.id, () => {
                return chakram.get(config.environment.url+"/"+user.id)
                .then(userData => {
                    console.log(user);
                    expect(userData.response.statusCode).to.equal(200)
                    return chakram.wait();
                });
            });
        })
    });
    

    It will be more accurate to try this way.