'Can't receive valid test on specific test controller

I'm testing a sign-in controller and therefore I've written the following test:

it("return 200 when user signed in successfully", async () => {
    await request(app)
      .post("/api/v1/signup")
      .send({
         name: "some-name",
         email: "[email protected]",
        password: "abcdefg",
})
      .expect(StatusCodes.CREATED);

    await request(app).post("/api/v1/signin")
.send({
         name: "some-name",
         email: "[email protected]",
        password: "abcdefg",
});
      .expect(StatusCodes.OK);
  });

The test code is straightforward. When the two controllers are tested in postman, everything work well and when start to test in jest, I receive bad request when a user try to sign in. The reason for this bad request is because when try to find an user by email in the signin controller, I receive null which I really don't understand what is the cause of this result.

What I must take into account to resolve this issue when testing in jest?



Solution 1:[1]

After doing a deep research into this problem, I had realised that when I use the supertest post-request to save particular data into an user collection, this doesn't seems to work. A workaround for this problem is to import the user-model to create particular data into the collection.

Updated Code

it("return 200 when user signed in successfully", async () => {
    await new User({
         name: "some-name",
         email: "[email protected]",
        password: "abcdefg",
}).save();

    const response = await request(app)
      .post("/api/v1/signin")
      .send({
         name: "some-name",
         email: "[email protected]",
        password: "abcdefg",
});

    expect(response.status).toBe(StatusCodes.OK);
  });

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