I encountered a timeout error while running a unit test case for a controller. I was mocking the request, response, and next, but jest threw an error during the test run.
Can anyone assist me in resolving this issue?
employee.controller.ts
import { Request, Response, NextFunction } from 'express';
import { employeeService } from './employee._service';
const empServices = new employeeService();
module employeeController {
export async function getEmployee(req: Request, res: Response, next: NextFunction) {
try {
const result = await empServices.getEmployee();
res.send(result);
return result;
}
catch (err) {
console.log(err);
res.statusCode = 200;
res.send("error in getEmployee: " + err);
};
};
}
export { employeeController }
employee.controller.spec.ts
import { Request, Response, NextFunction } from 'express';
import { employeeController } from '../employee.controller';
import { employeeService } from '../employee._service';
describe("should return pong message", () => {
const service = new employeeService();
it("should return pong message", async () => {
const mockRequest: any = {
body: jest.fn(),
params: jest.fn()
};
const mockResponse: any = {
json: jest.fn(),
status: jest.fn(),
};
const mockNext: NextFunction = jest.fn();
const spy = jest.spyOn(service, 'getEmployee').mockResolvedValueOnce([]);
const comments = await employeeController.getEmployee(mockRequest, mockResponse, mockNext);
expect(comments).toEqual([]);
expect(spy).toHaveBeenCalledWith()
expect(spy).toHaveBeenCalledTimes(1)
});
});
Thanks