Testing a flaky API in React - what is the right approach?
11:04 12 Feb 2024

I work on a React project (using CRA and TypeScript) that relies on an API that is rather flaky. It gets changed a lot by some other developer so things break frequently. I am really struggling to keep the frontend stable.

I have created TypeScript types that describe the JSON I expect from an API call.

Here is an example of such a type:

export type EnvDataResponse = {
    id: number;
    value: string;
}

Here is an example of fetching the corresponding data using Axios:

import axios from 'axios';

const api = {
  getEnvData: ():Promise => axios.get('/envData')
}

export default api;

I wonder now how I can test that API to ensure axios.get('/envData') returns a JSON that follows the data schema of type EnvDataResponse. I need some mechanism that warns me when the API all of a sudden sends back different data because the backend guy has changed the API without informing me.

Jest does not seem to be the right candidate as it only tests components and relies on a mock API, correct? I need to test the API itself, not components. Would implementing tests in Postman be the right approach?

reactjs jestjs postman