How to test endpoints using Jest #233
-
Given that express-zod-api is currently able to generate swagger documentation would it be possible to add test-data generation too or even full jest test generation? |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 2 replies
-
That's an interesting idea, @Isaac-Leonard. Perhaps there should be a property within the defaultEndpointsFactory.build({
// ...,
tests: [
{ request: {}, expectedResponse: {} }
]
}) I need to think a little bit more on it. |
Beta Was this translation helpful? Give feedback.
-
I was more thinking of integrating with something like |
Beta Was this translation helpful? Give feedback.
-
Perhaps a good start would be to simply expose the handler function on the Endpoint object after building it so it can be imported and called from test scripts without needing to provide full mockups of request and response objects. |
Beta Was this translation helpful? Give feedback.
-
I made a pull request where I've added a method to the endpoint class that runs the handler with a provided input and optional logger for use by tests |
Beta Was this translation helpful? Give feedback.
-
It might just be better to remove the protected modifier from the handler property though now that I think about it more. |
Beta Was this translation helpful? Give feedback.
-
Thank you @Isaac-Leonard , I will check out your PR |
Beta Was this translation helpful? Give feedback.
-
Thank you for your PR @Isaac-Leonard , your contribution and ideas are highly appreciated. Unfortunately I can not accept the offered solution due to the imbalance of efforts in relation to benefits. The handler may throw an error, but how exactly it will affect on the Endpoint — decides the ResultHandler, which may be customized by design as well. Therefore, testing handler is not enough. What should be tested is the public So, how to test endpoints.
Here is an example: const requestMock = {
method: "POST",
header: jest.fn(() => "application/json"),
body: {...},
};
const responseMock: Record<string, jest.Mock> = {
set: jest.fn(() => responseMock),
status: jest.fn(() => responseMock),
json: jest.fn(() => responseMock),
};
const configMock = {
cors: true, // or your actual config
};
await endpoint.execute({
request: requestMock as unknown as Request,
response: responseMock as unknown as Response,
config: configMock as CommonConfig,
logger: loggerMock,
});
expect(loggerMock.error).toBeCalledTimes(0);
expect(responseMock.status).toBeCalledWith(200);
expect(responseMock.json).toBeCalledWith({
status: "success",
data: {...},
}); That's it. Thank you once again for your contribution! P.S. Perhaps I will provide some sort of shorthand for mocking required entities, so it would take less efforts and be more obvious. |
Beta Was this translation helpful? Give feedback.
-
Sorry that I've been leaving issues by the way. |
Beta Was this translation helpful? Give feedback.
-
That looks really good |
Beta Was this translation helpful? Give feedback.
-
As of version 5.1.0 use the |
Beta Was this translation helpful? Give feedback.
As of version 5.1.0 use the
testEndpoint()
methodhttps://github.com/RobinTail/express-zod-api/blob/master/CHANGELOG.md#v510-beta1