retry-axios icon indicating copy to clipboard operation
retry-axios copied to clipboard

How to test in jest

Open maksha19 opened this issue 3 years ago • 4 comments

maksha19 avatar Mar 19 '21 06:03 maksha19

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 :)

JustinBeckwith avatar Mar 19 '21 17:03 JustinBeckwith

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.

ultrarunner avatar Aug 18 '21 17:08 ultrarunner