how to use this lib with async await?

This issue has been tracked since 2016-11-24.

I'm only getting Network Error from all responses

locnguyen wrote this answer on 2016-12-08

@luisrudge Have you resolved this yet? I am trying the same

luisrudge wrote this answer on 2016-12-08

yeah.. I'm using something like that:

import moxios from 'moxios';

describe('test', () => {
  beforeEach(() => moxios.install(axios));
  afterEach(() => moxios.uninstall(axios));
  it('does http request', (done) => {
    axios.get('https://google.com/'); //don't await this function
    moxios.wait(async() => {
        expect(moxios.requests.count()).toBe(1);
        const request = moxios.requests.mostRecent();
        await request.respondWith({ status: 200 });
        done();
      });
  });
});
createthis wrote this answer on 2017-11-22

@luisrudge and @locnguyen I think this makes more sense. Call moxios.wait before the method that you want to await. Doing it this way allows us to avoid calling done():

import moxios from 'moxios';

describe('test', () => {
  beforeEach(() => moxios.install(axios));
  afterEach(() => moxios.uninstall(axios));
  it('does http request', async () => {
    const data = { whatever: 1 };
    moxios.wait(() => {
      moxios.requests.count().should.eql(1); // sorry, I use shouldjs here
      const request = moxios.requests.mostRecent();
      request.respondWith({
        status: 200,
        response: data,
      });
    });
    let result = await axios.get('https://google.com/'); //make sure this is called after moxios.wait
    result.data.whatever.should.eql(1);
  });
});
onlywei wrote this answer on 2018-03-23

Here's how I'm doing it:

beforeEach(() => moxios.install());
afterEach(() => moxios.uninstall());

it('should work with async/await', async () => {
  makeSomeAxiosRequest();

  await respondWith(whatever);
  assert.equal(something, expectation);
});

function respondWith(response) {
  return new Promise((resolve, reject) => {
    moxios.wait(() => {
      moxios.requests.mostRecent().respondWith({
        status: 200,
        response,
      }).then(resolve, reject);
    });
  });
}
createthis wrote this answer on 2018-03-23

I've since switched to axios-mock-adapter. moxios was unreliable creating "Heisenbugs" in my tests that would happen intermittently. It was also quite a bit slower than axios-mock-adapter, which has been very reliable.

cpb wrote this answer on 2020-05-29

I have this defined near my moxios import, I like using it with stubRequest

const moxiosWait = () => new Promise((r) => moxios.wait(r));

Then, I can use stubRequest in before callbacks and have test examples like this:

it('should work with async/await', async () => {
  makeSomeAxiosRequest();

  await moxiosWait();
  assert.equal(something, expectation);
});
phillt wrote this answer on 2022-09-16

This works for me:

 it("axios tests", async function () {
        moxios.stubRequest("/tst/hr", {
            status: 200,
            responseText: "hey",
        });
        
        let callBack = sandbox.spy();
        axios.get("/tst/hr").then(callBack);
        
        sinon.assert.notCalled(callBack);
        
        await moxios.await();
        
        sinon.assert.calledOnce(callBack);
    });
More Details About Repo
Owner Name axios
Repo Name moxios
Full Name axios/moxios
Language JavaScript
Created Date 2016-05-25
Updated Date 2022-12-05
Star Count 1386
Watcher Count 20
Fork Count 85
Issue Count 19

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date