Testing API Endpoints with a Stub Server in Jest (TypeScript)
Testing API integrations can be tricky, especially when dealing with external dependencies. Creating a stub server allows you to isolate your code and test its behavior without relying on a real API, making your tests faster, more reliable, and easier to control. This challenge focuses on building a simple stub server using Node.js and testing it with Jest.
Problem Description
You need to create a minimal stub server using Node.js and the http module. This server should respond with a predefined JSON payload for a specific endpoint (/api/data). The goal is to create a server that can be easily mocked and tested within a Jest environment. Your task is to implement the server logic and then write a Jest test to verify that the server responds with the expected JSON data when the /api/data endpoint is accessed. The server should only handle GET requests.
Key Requirements:
- Endpoint: The server must respond to requests made to
/api/data. - Response: The server must respond with the following JSON payload:
{"message": "Hello from the stub server!"}. - HTTP Method: The server should only handle GET requests. Other methods should result in a 405 Method Not Allowed response.
- Error Handling: The server should handle invalid requests gracefully (e.g., requests to non-existent endpoints) and return a 404 Not Found response.
- TypeScript: The code must be written in TypeScript.
Expected Behavior:
- A GET request to
/api/datashould return a 200 OK status code and the specified JSON payload. - A GET request to any other endpoint should return a 404 Not Found status code.
- A request with a method other than GET (e.g., POST, PUT, DELETE) to
/api/dataor any other endpoint should return a 405 Method Not Allowed status code.
Examples
Example 1:
Input: GET request to /api/data
Output: 200 OK, {"message": "Hello from the stub server!"}
Explanation: The server successfully handles the GET request to the defined endpoint and returns the expected JSON.
Example 2:
Input: GET request to /api/other-endpoint
Output: 404 Not Found
Explanation: The server does not have a handler for this endpoint and returns a 404 error.
Example 3:
Input: POST request to /api/data
Output: 405 Method Not Allowed
Explanation: The server only handles GET requests. A POST request results in a 405 error.
Constraints
- The server should be lightweight and efficient.
- The code should be well-structured and easy to understand.
- The server should be written in TypeScript.
- The server should only listen on a single port (you don't need to specify the port in the code, it's for the Jest test to handle).
Notes
- You can use the built-in
httpmodule in Node.js to create the server. - Consider using a simple routing mechanism to handle different endpoints.
- The Jest test will need to start the server, make a request to it, and then verify the response. You'll need to use a library like
node-fetchoraxioswithin your Jest test to make the HTTP request. - Remember to properly handle errors and return appropriate status codes.
- Focus on creating a minimal, functional stub server that meets the specified requirements. Don't over-engineer the solution.