So i have a simple express route which I am trying to test using Jest. Below are the code snippets
auth.route.ts
import * as authController from '../controllers/auth.controller';
const router = express.Router();
// POST /api/user/register
router.post(`${userApis.register}`, authController.register);
export { router };
auth.route.test.ts
it('register', async() => {
const spy = jest.spyOn(authController, "register");
await request(app).post('/api/user/register');
expect(spy).toHaveBeenCalled();
});
But the test fails. Below is the error.
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
One way i got it working is re writing my route as below but that is not the right way i think
import * as authController from '../controllers/auth.controller';
const router = express.Router();
// POST /api/user/register
router.post(`${userApis.register}`, (req, res, next) => {
authController.register(req, res, next);
});
export { router };
What would be solution for this. I am new to Jest. Thanks in Advance