retry-axios
retry-axios copied to clipboard
How to test in jest
Greetings! What have you tried? Did you run into specific issues? I don't use Jest, so this isn't something I'm likely to dig into :)
wondering the same thing, if I want to test a function that uses axios with a retry config how can write my test to make sure the request is being retried.
In req.js
const sendRequest = async function () {
rax.attach()
await axios.post(
`https://domain.com`,
body,
{
raxConfig: {
retry: 3,
noResponseRetries: 3,
httpMethodsToRetry: ['POST']
}
}
)
}
module.exports = {
sendRequest
}
In test.spec.js
const axios = require('axios')
const { sendRequest } = require('../src/req.js')
describe('sendRequest', () => {
it('retries up to max retry amount (3)', async () => {
await sendRequest()
// Is there a way to asset this?
expect(axios.post).toHaveBeenCalledTimes(3)
})
})
I found a solution to my issue by using nock
without mocking axios, thanks anyways
I have the same issue. I posted a question on stackoverflow here: https://stackoverflow.com/questions/68836524/jest-unit-test-and-retry-axios-node-js Appreciate any feedback on what I am doing wrong. Thank you so much.