Search code examples
node.jsmocha.jssupertest

How to store the response body for further test case in supertest


I have an API /user/login which returns the token.

using supertest and mocha i wrote a test case where it will make a call to '/user/login' and verify that the response has a token or not

now i have to store the token for further token for further test case

How can i achieve that


Solution

  • I hope this example will help you.

    describe("Example with token", function () {
      let token;
    
      before(function (done) {
        //getting token logic
        token = tokenValue;
        done();
      });
    
      it("first test", function (done) {
        supertest.request(app)
          .get("/route")
          .set("TokenHeader", token)
          .expect(200)
          .end(done);
      });
    });