๐ Feature Proposal
The FakeApi
class will be responsible for creating fake, but not totally fake, endpoints using fastify. This class should have an enjoyable API to create new endpoints, it should be very easy to create endpoints that return the following specific data in the response:
It would be very nice if FakeApi
helper could be configured using .json
files, the same way that Wiremock does. Let's suppose that we have created the resources/fakeapi/create-user.json
file:
When calling the await FakeApi.start()
helper, it should read the resources/fakeapi
folder and register all the routes inside it. The create-user.json
content would have the route path, method, status code, headers and body defined for the response:
{
"path": "/users",
"method": "POST",
"statusCode": 201,
"headers": {
"Content-Type": "application/json"
},
"body": {
"id": 1,
"name": "Robson Trasel"
}
}
Motivation
Sometimes the developer needs to create more realistic tests, where he really needs to make a Http request. We have tryed to use Wiremock for this purpose, but is very hard to configure it for Node.js inside pipelines like Github Actions.
Example
Here is an example of using the FakeApi
helper API in runtime:
const path = '/users'
const statusCode = 200
const user = { id: 1, name: 'Joรฃo Lenon' }
const arrayOfUsers = [user]
const headers = {
Content-Type: 'application/json'
}
await FakeApi.builder()
.method('GET')
.path(path)
.statusCode(statusCode)
.body(arrayOfUsers)
.headers(headers)
// Register the fake route and clear FakeApi static state:
.register()
const port = 8080
await FakeApi.start(port)
// Requesting to the endpoint we have created:
const users = await HttpClient.get('http://localhost:8080').json()
// Stop the fake server:
await FakeApi.stop()