2

I'm having issues getting stubRequest to work properly. Here's my code:

it('should stub my request', (done) => {
    moxios.stubRequest('/authenticate', {
        status: 200
    })

    //here a call to /authenticate is being made
    SessionService.login('foo', 'bar')

    moxios.wait(() => {
        expect(something).toHaveHappened()
        done()
    })
})

This works fine:

it('should stub my request', (done) => {
    SessionService.login('foo', 'bar')

    moxios.wait(async () => {
        let request = moxios.requests.mostRecent()

        await request.respondWith({
            status: 200
        })

        expect(something).toHaveHappened()

        done()
    })
})

The second method just get's the last call though, and I'd really like to be able to explicitely stub certain requests.

I'm running Jest with Vue.

Canta
  • 1,480
  • 1
  • 13
  • 26
Rinux
  • 815
  • 1
  • 9
  • 18

2 Answers2

2

I landed here with a similar goal and eventually solved it using a different approach that may be helpful to others:

moxios.requests has a method .get() (source code) that lets you grab a specific request from moxios.requests based on the url. This way, if you have multiple requests, your tests don't require the requests to occur in a specific order to work.

Here's what it looks like:

moxios.wait(() => {
  // Grab a specific API request based on the URL
  const request = moxios.requests.get('get', 'endpoint/to/stub');
  
  // Stub the response with whatever you would like
  request.respondWith(yourStubbedResponseHere)
    .then(() => {

      // Your assertions go here

      done();
    });
});

NOTE: The name of the method .get() is a bit misleading. It can handle different types of HTTP requests. The type is passed as the first parameter like: moxios.requests.get(requestType, url)

Tyler Auer
  • 117
  • 2
  • 8
0

it would be nice if you show us the service. Service call must be inside the moxios wait func and outside must be the axios call alone. I have pasted a simplified with stubRequest

describe('Fetch a product action', () => {
    let onFulfilled;
    let onRejected;

    beforeEach(() => {
        moxios.install();
        store = mockStore({});
        onFulfilled = sinon.spy();
        onRejected = sinon.spy();
    });

    afterEach(() => {
        moxios.uninstall();
    });

    it('can fetch the product successfully', done => {
            const API_URL = `http://localhost:3000/products/`;

            moxios.stubRequest(API_URL, {
                status: 200,
                response: mockDataSingleProduct
            });

            axios.get(API_URL, mockDataSingleProduct).then(onFulfilled);

            const expectedActions = [
                {
                    type: ACTION.FETCH_PRODUCT,
                    payload: mockDataSingleProduct
                }
            ];

            moxios.wait(function() {
                const response = onFulfilled.getCall(0).args[0];
                expect(onFulfilled.calledOnce).toBe(true);
                expect(response.status).toBe(200);
                expect(response.data).toEqual(mockDataSingleProduct);

                return store.dispatch(fetchProduct(mockDataSingleProduct.id))
                .then(() => {
                    var actions = store.getActions();
                    expect(actions.length).toBe(1);
                    expect(actions[0].type).toBe(ACTION.FETCH_PRODUCT);
                    expect(actions[0].payload).not.toBe(null || undefined);
                    expect(actions[0].payload).toEqual(mockDataSingleProduct);
                    expect(actions).toEqual(expectedActions);
                    done();
                });
            });
        });
})