I mostly agree with @Alex.
But usually, rather than always moving data to a different file, I prefer to keep real API mock data for one specific case inside the test file itself.
I treat them like the PropType section of React Components, keeping them at the button of the test file (__test__, __spec__, *test.js or *.spec.js) but inside the file unless I have to share.
Here a super simple example with axios:
src/__mocks__/axios.js
export default {
get: jest.fn(() => Promise.resolve({ data: [] })),
};
Then axios is mocked, because of that now if we want to test a super simple wrapper API utility like:
import Axios from 'axios';
export const yourMethod = async () => {
return new Promise(resolve => {
Axios.get(`yourAPIEndPoint`)
.then(result => {
resolve([...result.data]);
})
.catch(e => {
console.error('should treat the error', e);
});
});
};
For me the one test could only be:
import Axios from 'axios';
import { yourMethod } from './borrame';
describe('yourMethod TestCase', () => {
it('it returns the data you expect', async () => {
// Specific response for this test case.
Axios.get.mockImplementationOnce(() =>
Promise.resolve({
data: yourMethodMockData,
})
);
const result = await yourMethod();
expect(result[0].id).toBe(yourMethodRawMockData[0].id);
});
});
/**
* Specific Test Data
*/
const yourMethodMockData = [
{
id: '12345',
name: 'Name for 12345',
},
];
Even though, if the same mock data is going to be reused outside the test files, I move this mock data to different .js inside a __test__ folder and export it to be reused.
I have even created mockAPIGenerators when I have to "generate" the .json data based on API call params, but for me the general rule to follow is always to KIS
So at the button as a constant in the same test file, unless it's not enough for you .