ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to test redirect graphql api using supertest

raviSussol opened this issue · comments

I have a bit different scenario to test graphql api. I have a login/logout auth api which at first listens /api/v1/graphql and then it redirects to the/graphql route for login/logout request. Below are mutations for login and logout and if I use below code to test this login auth then the test for login happens successfully and the cookie with session is also set successfully. But when I try to test logout mutation then it fails to test it successfully. The reason is the cookie with session gets disappeared when logout mutation is queried. But this login/logout graphql api just works within the app or if I test with any rest client app. Am I missing something here. Can somebody pin-point any missing steps or error in the code. That would be really helpful. 🙏

// login test
const mut = `
  mutation {
    login(input: { username: "test", password: "test" }) {
      success
      message
    }
  }
`;
const loginResponse = await request(app.server)
  .post('/api/v1/graphql')
  .set('Accept', 'application/json')
  .send({ query: mut })
  .redirects(1);
const {
  body: {
    data: { login },
  },
} = loginResponse;
expect(loginResponse.status).toBe(200);
expect(loginResponse.headers['content-type']).toMatch(/json/);
expect(login.success).toBe(true);

// logout test
const logoutMut = `
  mutation {
    logout {
      success
      message
    }
  }
const logoutResponse = await request(app.server)
  .post('/api/v1/graphql')
  .set('Accept', 'application/json')
  .send({ query: logoutMut })
  .redirects(1);
const {
  body: {
    data: { logout },
  },
} = logoutResponse;
expect(logoutResponse.status).toBe(200);
expect(logoutResponse.headers['content-type']).toMatch(/json/);
expect(logout.success).toBe(true);
`;

Btw, I'm using fastify for nodejs server.

Nevermind, I got workaround for this.