axios / moxios

Mock axios requests for testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does one mock axios.all([]) requests?

CallumBrankin opened this issue · comments

I'm came across a problem while i was setting up mocking for my project.

I have an axios request that that uses axios.all to make several requests at the same time. Then i thought "how the hell am i going to mock this?".

I'd need to mock about 10 requests to mock the axios.all request, and i don't think that would even work.
Is there functionality to mock an axios.all request?

Update:

I've mocked each request indiviually but is a unefficient and painful process, maybe there's a better way?

Kamino cloned this issue to anilanar/moxios

if anyone is interested, here is a simple function to handle this

moxios.respondAllWith = function () {
  return Array.from(arguments).reduce((promise, res, i) => {
    return moxios.requests.at(i).respondWith(res)
  }, null)
}

then in your tests

moxios.respondAllWith(
  {
    status: 200,
    response: {}
  },
  {
    status: 200,
    response: {}
  }
).then(() => {
 // done()
})

Property 'respondAllWith' does not exist on type.....

if anyone is interested, here is a simple function to handle this

moxios.respondAllWith = function () {
  return Array.from(arguments).reduce((promise, res, i) => {
    return moxios.requests.at(i).respondWith(res)
  }, null)
}

then in your tests

moxios.respondAllWith(
  {
    status: 200,
    response: {}
  },
  {
    status: 200,
    response: {}
  }
).then(() => {
 // done()
})