axios / moxios

Mock axios requests for testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mix real requests with mock requests ?

jacksonkr opened this issue · comments

Scenario

During development I am using stubRequest to return mock data for api endpoints that I know are incomplete.

Problem

On urls where there is no stub request to intercept the fetch, the fetch still never goes through - almost like moxios is catching EVERYTHING that goes through axios and not just the urls that match one of the stub requests.

Question

How can I have SOME fetches (which match a stubRequest) return mock data while allowing other fetches (which don't match any stub requests) return real data?

code

        moxios.install();

        const stubs = {};
        //todo: make top three expressions pull from constants @jkr
        stubs["^navigationMock"] = navigationMock;
        stubs["^ancillaryMock"] = ancillaryMock;
        stubs["^pageMock"] = pageMock;
        stubs[constants.EVENT_CALENDAR_HREF] = eventCalendarMock;
        stubs[constants.STYLE_GUIDE_HREF] = styleGuideMock;

        for (const key in stubs) {
            const re = new RegExp(key);
            const mock = stubs[key];
            // console.log(key, re, stubs[key])
            moxios.stubRequest(re, {
                status: 200,
                response: mock
            });
        }
        // console.log(moxios);

        axios.create();

It seems like moxios isn't really suited for this so I'm looking to go with axios interceptor instead