chaijs / chai-http

HTTP Response assertions for the Chai Assertion Library.
http://chaijs.com/plugins/chai-http
633 stars 113 forks source link

[possibly bug] aws-sdk and multer-s3 with chai-http agent promise not resolved #298

Closed fzn0x closed 3 years ago

fzn0x commented 3 years ago

I'm using https://github.com/anacronw/multer-s3 with https://www.npmjs.com/package/aws-sdk

there is something odd when I use chai-http which leads to an API that uses middleware with those libraries, where if I call the done function after the expect code or return an async function in the test function it, the middleware function from the library does not run.

However when I call the done function before the expect code, the middleware function of it runs but none of the test function from chai is work, is this could be considered as a bug?

    agent
      .post("/api/auth/login") // to get jwtToken
      .send({
        email: "cantbehacked@gmail.com",
        password: "fakepasswordforgithubissue",
      })
      .then((res) => {
        return agent
          .post("/api/documents")
          .set(CONFIG.tokenHeaderKey, `Bearer ${res.body.token}`)
          .attach("cool_sample1", "./test/samples/sample-number-1-bro.jpg")
          .attach("cool_sample2", "./test/samples/sample-number-2-bro.jpg")
          .attach("cool_sample3", "./test/samples/sample-number-3-bro.jpg")
          .then((res) => {
            done();
            console.error(res.body);
            expect(res).to.have.status(200);
          });
      })
      .catch(function (err) {
        throw err;
      });
fzn0x commented 3 years ago

I'll close this issue, the solution is to use .then(done,done), the reason for that is because agent itself returning two promises in that one test case.

    agent
      .post("/api/auth/login") // to get jwtToken
      .send({
        email: "cantbehacked@gmail.com",
        password: "fakepasswordforgithubissue",
      })
      .then((res) => {
        return agent
          .post("/api/documents")
          .set(CONFIG.tokenHeaderKey, `Bearer ${res.body.token}`)
          .attach("cool_sample1", "./test/samples/sample-number-1-bro.jpg")
          .attach("cool_sample2", "./test/samples/sample-number-2-bro.jpg")
          .attach("cool_sample3", "./test/samples/sample-number-3-bro.jpg")
          .then((res) => {
            console.error(res.body);
            expect(res).to.have.status(200);
          }).then(done, done);
      })
      .catch(function (err) {
        throw err;
      });

I hope it helps everyone :)