idiot on ruby idiot on ruby - 1 month ago 15
Node.js Question

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

Answer

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);
  });
});