'I am trying to set a cookie in supertest but it does not work

I am trying to set a cookie session to a post request in supertest but I cannot. This is my test code:

const app = express();

app.set("trust proxy", true);
app.use(json());
app.use(
  cookieSession({
    signed: false,
    secure: process.env.NODE_ENV !== "test",
  })
);
   
    
    it("this is a test", async () => {
      const response = await request(app)
        .post("/api/users/current")
        .set("Cookie", [
        'express:sess=eyJqd3QiOiJleUpoYkdjaU9pSklVekkxTmlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKcFpDSTZJakV5TXpRaUxDSmxiV0ZwYkNJNkluUmxjM1JBZEdWemRDNWpiMjBpTENKcFlYUWlPakUyTkRZeE5qazVNREI5LjZybE8zODB2RG1PN0J4cFlhRERZSnBScmhrMEc2X3pvN3BBd2MxYU5rMVEifQ=='
          ])
        .send({});
    
      expect(response.get("Set-Cookie")).toBeDefined();
    });

and this test fails because response.get("Set-Cookie") is undefined



Solution 1:[1]

According to my investigation, Supertest library is not stable. Majority developers have difficulties to set or retrieve cookies during testing.

You can take a look at this link: how to set signed cookies

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dafny