Skip to content

Instantly share code, notes, and snippets.

@takethefake
Created March 8, 2019 09:35
Show Gist options
  • Save takethefake/065348c5281045e24772927a8ad97da6 to your computer and use it in GitHub Desktop.
Save takethefake/065348c5281045e24772927a8ad97da6 to your computer and use it in GitHub Desktop.
// src/__mocks_axios.js
const mockAxios = jest.genMockFromModule('axios')
// this is the key to fix the axios.create() undefined error!
mockAxios.create = jest.fn(() => mockAxios)
export default mockAxios
// example test class which uses axios mock
import mockAxios from 'axios'
test('can add Todo to List', async () => {
mockAxios.get.mockImplementationOnce(() =>
Promise.resolve({
data: [],
}),
)
mockAxios.post.mockImplementationOnce(() =>
Promise.resolve({
data: [new Todo('test')],
}),
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment