Currently, I am performing tests using mocks to avoid always hitting APIs. Nevertheless, I also aim to introduce a condition that would allow me to utilize the same test for API testing purposes.
However, when I include a condition, it seems to be disregarded and the mocks are never applied regardless of whether the condition is true or false.
import config from 'config';
if(!config.test.useNetwork) {
jest.mock('api/companies');
jest.mock('api/articles');
}
import { searchCompany } from 'api/companies';
...
I have a couple of questions:
- Do you have any suggestions on how to implement conditional mocks? I suspect that mocks may not apply because
jest.mock
needs to be invoked before any imports? - What is typically considered as the best practice for network testing? If I solely rely on mocks, essentially I am only testing the mocks and not the actual network request code. However, omitting mocks means unnecessary API calls are being made.