I am currently developing an API that uses a GET request on /api/flights/ to retrieve an array of Flights. All flights in the array have the same properties. I am looking for a way to verify if the flights list in my response.body matches an array of the same type, Flight. Is there a method in Jest to achieve this?
test('Show flight', async () => {
const response = await supertest(app.express)
.get('/api/flights/')
.expect(200);
//Here I want to validate if the response.body matches an array of Flight type
});
This is what my response.body looks like:
{
"flights": [
{
"_id": "6300bee627fff32a054f5355",
"name": "Kiyv Madrid",
"from": "Kiyv",
"destination": "Madrid",
"transplants": [],
"departureDate": "20.08.2022-09:30",
"arrivalDate": "21.08.2022-01:00",
"seatsCount": 150,
"createdAt": "2022-08-20T11:00:54.844Z",
"updatedAt": "2022-08-20T11:00:54.844Z",
"__v": 0
},
{
"_id": "6300bf0127fff32a054f5358",
"name": "Kyiv Barcelona",
"from": "Kyiv",
"destination": "Barcelona",
"transplants": [],
"departureDate": "20.08.2022-09:30",
"arrivalDate": "21.08.2022-01:00",
"seatsCount": 150,
"createdAt": "2022-08-20T11:01:21.853Z",
"updatedAt": "2022-08-20T12:45:42.596Z",
"__v": 0
},
{
"_id": "6300bf2a1df953f67f0e79b5",
"name": "Kyiv Madrid",
"from": "Kyiv",
"destination": "Madrid",
"transplants": [],
"departureDate": "20.08.2022-09:30",
"arrivalDate": "21.08.2022-01:00",
"seatsCount": 150,
"createdAt": "2022-08-20T11:02:02.154Z",
"updatedAt": "2022-08-20T11:02:02.154Z",
"__v": 0
}
]
}
flight.test.interface.ts
interface Flight {
_id: string;
name: string;
from: string;
destination: string;
transplants: string;
departureDate: string;
arrivalDate: string;
seatsCount: number;
__v: number;
}
export default Flight;