Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test in jest #144

Open
maksha19 opened this issue Mar 19, 2021 · 4 comments
Open

How to test in jest #144

maksha19 opened this issue Mar 19, 2021 · 4 comments
Labels
question Further information is requested

Comments

@maksha19
Copy link

No description provided.

@JustinBeckwith JustinBeckwith added the question Further information is requested label Mar 19, 2021
@JustinBeckwith
Copy link
Owner

JustinBeckwith commented Mar 19, 2021

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

@alejandro-delgado-garcia

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

@alejandro-delgado-garcia
Copy link

alejandro-delgado-garcia commented Jun 17, 2021

I found a solution to my issue by using nock without mocking axios, thanks anyways

@ultrarunner
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants