Search code examples
javascriptnode.jsmocha.jsweb-api-testingchakram

Unable to expect status in chakram when using .then in before()


I am using chakram to test API and whenever I am using a .then in a before(), I always get an error when testing the "it". I am not sure what I am doing wrong but I expect it has something to do with my returns.

describe('create group', function() {
    before('test', function() {
        return banana = chakram.request("POST", `${url}`, {headers, body}
         }).then(function (json) {
            test = json.body
         })
    })
    it("should return a 200 status when creating groups", function () {
        console.log(test)
        return expect(banana).to.have.status(201)
    })
})

The error returned is TypeError: Cannot read property 'response' of undefined


Solution

  • Before the answer comes, you throw the non-answer to banana.

    That's why the banana is hollow.

    describe('create group', () => {
        let bananaResponse;
        before('test', () => {
            return chakram.request("POST", `${url}`, {headers, body}
             ).then((responseJson) => {
                bananaResponse = responseJson;
             })
        })
        it("should return a 200 status when creating groups", () => {
            console.log(bananaResponse.body);
            return expect(bananaResponse).to.have.status(201);
        })
    })
    

    You can try it.