Here is a health check function that I am working with:
export default function handler(req, res) {
res.status(200).json({ message: "Hello from Next.js!" });
}
Alongside this function, there is a test in place:
import handler from "./healthcheck"
describe("Healthcheck", () => {
test("verifying the application is live and returning status 200", () => {
const mockFn = jest.fn({
status: jest.fn(),
json: jest.fn()
});
expect(mockFn).toHaveBeenCalledWith();
expect(mockFn.status).toBe(200);
});
});
While testing, the main goal is to ensure that the function is being executed and the response status is set to 200. To achieve this, it is necessary to properly mock out the functions within the request and response objects.