-1

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: "test@mail.com",
        password: "abcdefg",
})
      .expect(StatusCodes.CREATED);

    await request(app).post("/api/v1/signin")
.send({
         name: "some-name",
         email: "test@mail.com",
        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?

Dafny
  • 71
  • 5
  • Without a [mre], we can't possibly say. – jonrsharpe May 02 '22 at 08:46
  • @jonrshape, you are correct, that is what I'm doing at this moment, sorry for that. – Dafny May 02 '22 at 18:36
  • Please note this is still not a MRE. `app`, `StatusCodes`, `UserMode`, `BadRequest`, `Password` are all undefined. The file structure and imports/exports aren't shown. Dependency versions are unclear. A MRE looks like this: https://stackoverflow.com/q/68024935/3001761. Unless we can recreate the problem locally, we likely cannot help you to solve it. – jonrsharpe May 04 '22 at 16:39
  • Thank you @jonrsharpe. I have updated it as requested. If I have forgot something out, please leave a comment and I will get back to it. – Dafny May 04 '22 at 21:14
  • This is feasibly _reproducible_, but unlikely to be _minimal_. Do I really need to spin up a MongoDB? Are all 20 dependencies relevant? If absolutely nothing else, `it.skip("", () => true);` is clearly redundant. – jonrsharpe May 04 '22 at 21:16
  • I don't have any idea if the depencies are relevant. Maybe I'm using a old version of express. If i remove the it.skip, I receive an error which indicates that the test suite must contain at least one test. That is the reason why I put the it.skip. You don't need to spin up a MongoDB. For the test I'm using mongodb-memory-server which is more efficient than MongoDB. – Dafny May 04 '22 at 21:48
  • Ah, I see; that's because the file's named `setup.test.js`, which is expected to contain tests - if you named it `setupTest.js` instead you wouldn't need a stub test. And I'm afraid despite being too much code it's also still not actually enough - many things are missing. You need to be able to copy-paste these files into an empty directory and recreate the issue. – jonrsharpe May 04 '22 at 22:02
  • 1
    Oke, Thank you @jonrsharpe. Tomorrow when I wake up with a fresh mind, I will do as you recommended. Thank you. I will get back to this problem.. – Dafny May 06 '22 at 17:12

1 Answers1

0

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: "test@mail.com",
        password: "abcdefg",
}).save();

    const response = await request(app)
      .post("/api/v1/signin")
      .send({
         name: "some-name",
         email: "test@mail.com",
        password: "abcdefg",
});

    expect(response.status).toBe(StatusCodes.OK);
  });
Dafny
  • 71
  • 5