Skip to content

Instantly share code, notes, and snippets.

@Sidnioulz
Created November 30, 2021 13:34
Show Gist options
  • Save Sidnioulz/5ae2439e683a670ccbdf51dae2da1417 to your computer and use it in GitHub Desktop.
Save Sidnioulz/5ae2439e683a670ccbdf51dae2da1417 to your computer and use it in GitHub Desktop.
Basic backend API mock functions
import axios from 'axios'
jest.mock('axios')
export const mockAxiosSuccess = (data, status = 200) => {
axios.mockResolvedValueOnce({ data, status })
}
export const mockAxiosFailure = (data, status = null) => {
axios.mockRejectedValueOnce({
response: {
data,
status: status || data?.code || 400,
},
})
}
export const mockAxiosNetworkFailure = () => {
const err = new Error('Network Error')
err.isAxiosError = true
err.config = { url: 'badurl.com/bad/path' }
axios.mockRejectedValueOnce(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment