unit-testingmocha.jschai-as-promised

How to check promise value and property updated in promise but not returned with chai-as-promised


I have a promise which checks if I'm authorized or not (returning true or false). Within that promise, when authorization is false, I'm also adding the 403 statusCode to the response object that I pass as a parameter. I'm testing the result of my promise with chai-as-promised but I didn't manage to find a way to also test the status code of the response after the promise is resolved.

var result = authorizePromise(request, response);
return expect(result).to.eventually.equal(false).and(response.statusCode.should.be(403));

Solution

  • the response should be used in parent scope.
    and the assertion should be called after the promise is ready
    so it should be a function that called after.
    your code example .and(response.statusCode.should.be(403)); runs the assertion instant, and passes it as parameter to .and() it does not wait for the promise.

    *You can use .andinstead of .then too.

    var response = {};
    var resultPromise = authorizePromise(request, response);
    return expect(resultPromise).to.eventually.equal(false)
        .then(function(){return response.statusCode.should.be(403)})