Giter Site home page Giter Site logo

Comments (7)

onlywei avatar onlywei commented on April 28, 2024 5

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

from moxios.

createthis avatar createthis commented on April 28, 2024 4

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.

from moxios.

cpb avatar cpb commented on April 28, 2024 3

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

from moxios.

createthis avatar createthis commented on April 28, 2024 1

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

from moxios.

locnguyen avatar locnguyen commented on April 28, 2024

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

from moxios.

luisrudge avatar luisrudge commented on April 28, 2024

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

from moxios.

phillt avatar phillt commented on April 28, 2024

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

from moxios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.