These are chat archives for dareid/chakram

28th
Dec 2015
Troy Hawley
@troywoy
Dec 28 2015 22:36

I'm trying to create nested requests inside of a promise but having a hard time getting the test to wait or complete.

For example, if I have the following:

it('waits for the inner request', function() {
    return chakram.get(url).then(function(response1) {
        return chakram.get(url + '/' + someId).then(function(response2) {
            expect(response2).to.be.an('object');
        });
});

The test completes before the second request ever returns and therefore the internal logic can never fail the test.

If I try using done:

it('waits for the inner request with manual done call', function(done) {
    return chakram.get(url).then(function(response1) {
        return chakram.get(url + '/' + someId).then(function(response2) {
            expect(response2).to.be.an('object');
            done();
        });
});

The test never completes and I reach "timeout, are you sure done is being called...". Anyone have hints?

Ignore the missing });, just a typo into Gitter.
Troy Hawley
@troywoy
Dec 28 2015 22:56
I think I answered my own question... I dislike how the README is formatted so it was hard to determine that it was chained on the original request and not nested inside of each then promise method. I moved the code around as follows, and it seems to work:
it('works sequentially', function() {
    return chakram.get(url).then(function(response1) {
        return chakram.get(url + '/' + someId);
    }).then(function(response2) {
        expect(response2).to.be.an('object');
    });
});
Daniel Reid
@dareid
Dec 28 2015 23:08
Hi, either should work. You need to ensure u return a promise for the tests. So for the nested example if you return the promise returned from the expect statement it should wait correctly.
Well actually, taking another look, it should work as you are returning a promise with a then. Could you provide some example code using http bin?
Do you have any suggestions for improving the readme?
Troy Hawley
@troywoy
Dec 28 2015 23:15
I just disliked how the "should support sequential API interaction" test was "smushed" all together without much whitespace or indentation. As far as an example goes, I can through one together later if you'd like. I've already rewritten all my tests to work using the chained then approach, so I'd need time to go back and get a runnable version of my previous code which I don't have time for right this moment. Looking back at it, the issue may have been that my internal get() was inside of expect(response1).to.have.json(function(json) { ... }); which likely was the root of my issue.
Troy Hawley
@troywoy
Dec 28 2015 23:21
Darn user error... that looks like what it was. The inner get() was inside a .to.have.json so I could verify the first response and grab some data from it (primarily id values) and I didn't realize my return was returning to the to.have.json function and not the then function.