I'm running tests with mocks so that the tests don't always hit APIs. I however also want to add a condition, so that I can use the same test to tests the APIs.
When I however add a condition, it ignores it and never applies the mocks no matter if 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';
...
Two questions:
- Any idea for how to add conditional mocks? I think mocks never apply because
jest.mockmight need to be called before any imports? - What's the convention for testing over the network? If I just use mocks, I'm really just testing the mocks, and not the network request code. But if I don't use mocks I'm unnecessarily hitting the API.