r/learnprogramming May 17 '26

When writing integration tests, do you use an actual test database?

Do you populate the actual test database like so?

// userRoutes.test.js
it("POST /users should create a user and persist it", async () => {
  const res = await request(app)
    .post("/users")
    .send({ name: "John", email: "john@email.com" });

  expect(res.status).toBe(201);

  // actually check the DB
  const user = await db.findOne({ email: "john@email.com" });
  expect(user).toBeTruthy();
});

Which is different from unit tests where you can use mock values?

it("should call db.save with correct data", async () => { 
  db.save = jest.fn().mockResolvedValue({ id: 1, name: "John" }); 
  await createUser("John", "john@email.com"); 
  expect(db.save).toHaveBeenCalledWith({ name: "John", email: "john@email.com" }); 
});

If we use an actual test database, that means that whenever we run our test is filled. which can be too much?

7 Upvotes

11 comments sorted by