chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

Test reports that it both passes and fails during sequential async operations

Open josh-g3 opened this issue 7 years ago • 0 comments

I'm writing an integration test to make sure the rate limiter middleware on my app is working. This necessitated sequential Promise execution to keep track of how many requests have been sent, since that affects the desired behavior in each pass. So I'm using Bluebird's mapSeries() function for this, and getting some weird output.

Here's my test code:

it(`should allow only ${allowedRequestCount} requests within frequency window`, function(done) {
    this.timeout(0);

    Promise.mapSeries(_.range(1, allowedRequestCount + 10), iter => {
      return chai
        .request(baseUri)
        .get(resourcePath)
        .set({
          'content-type': 'application/json; charset=utf-8',
          'transfer-encoding': 'chunked',
        })
        .send()
        .then(res => {
          if (iter <= 100) {
            expect(res.body).to.deep.equal(expectedResponse);
          }

          done();
        })
        .catch(err => {
          if (iter > 100) {
            expect(err).to.have.property('status', 429);
            expect(err).to.have.property('message', 'Too Many Requests');
          }

          done();
        });
    });
  });

Which causes Mocha to yield this: screen shot 2018-02-12 at 10 26 23 am

josh-g3 avatar Feb 12 '18 17:02 josh-g3