> main landing file
//app.js
const route = require("./routes/app-route");
app.use('/api', route);
> routes are defined in this file
//app-route.js
router.get('/users', [authJwt.verifyToken], usersController.users);
> trying to write unit test case for this file with code coverage 100% so here i need to mock the User.find({}) to be empty once and return list of data another time.
//user-controller.js
exports.users = (req, res) => {
User.find({})
.then(users => {
if (users.length === 0) {
return res.status(404).send({ message: "Users Not found." });
}
res.status(200).send({
message: "Users Found",
status: "success",
users
});
})
.catch(err => {
res.status(500).send({ message: err.message });
});
};
I wanted to write test case for node js controller. wrote this piece of code but didn't worked but
test("find users with empty array", async () => {
// arrange and act
const mReq = {};
const mRes = {};
User.find = jest.fn().mockImplementation(() => {
return Promise.resolve([]);
});
let result = await UsersContoller.users(mReq, mRes);
expect(result)
});
I want to add expect conditions like status 200, 404, etc expect(result.status).toBe(404)