2.1.1 • Published 8 months ago
@exodus/mock-fetch v2.1.1
@exodus/mock-fetch
Utils to help you mock HTTP responses in your tests
Usage
mockFetchival
If your codebase uses fetchival
, you can use this function to mock your API calls.
import { mockFetchival } from '@exodus/mock-fetch'
import spiderman from '..'
describe('Spiderman', () => {
beforeEach(async () => {
await mockFetchival(`${__dirname}/__mocks__/mock-spiderman.json`)
})
it('should get uncle Ben name from API', async () => {
const bensPhone = await spiderman.getBenPhoneFromAPI()
expect(bensPhone).toMatchSnapshot()
})
})
The code above will perform one API call the first time you run your tests and store the results. All subsequent tests will read the results from the store making sure your tests:
- Run faster.
- Don't depend on real time values (e.g. prices)
Notes
- If you want your tests to react to real-time changes (e.g. you want a test to fail if an API response changes), we suggest not mocking the response.
Troubleshooting
If your tests are nor reading/writing from the mocks JSON file, ensure the @exodus/fetch
version your package is using is the same one as this library's. Otherwise jest will not use the mock exposed here when your codebase tries to do an actual fetch.