How to chain http calls with superagent/supertest?

后端 未结 4 1838
一个人的身影
一个人的身影 2021-02-01 13:28

I am testing an express API with supertest.

I couldn\'t get multiple requests in a test case to work with supertest. Below is what i tried in a test case. But the test c

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 13:42

    This can be most elegantly solved with promises, and there's a really useful library to use promises with supertest: https://www.npmjs.com/package/supertest-as-promised

    Their example:

    return request(app)
      .get("/user")
      .expect(200)
      .then(function (res) {
        return request(app)
          .post("/kittens")
          .send({ userId: res})
          .expect(201);
      })
      .then(function (res) {
        // ... 
      });
    

提交回复
热议问题