Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axios.mockErrorFor #55

Closed
Emidomenge opened this issue Jun 30, 2020 · 2 comments
Closed

axios.mockErrorFor #55

Emidomenge opened this issue Jun 30, 2020 · 2 comments

Comments

@Emidomenge
Copy link

Hello,

First of all, great job for this lib, I like it a lot. :)

Just a request if it's possible to do a function axios.mockErrorFor which will behave same as axios.mockResponseFor but sending an error ?

Or is there a workaround to do it ?

Thanks !

@kingjan1999
Copy link
Collaborator

Hi,

you should be able to use getReqMatching to get the request info and then use it as a second parameter for mockError (see docs) to replicate the behavior of mockResponseFor.

Adding mockErrorFor might be interesting for symmetry reasons, but it would only be a mere abstraction over existing methods as described above.

Hope this helps for now...

@Emidomenge
Copy link
Author

Emidomenge commented Oct 20, 2020

Ok, it works indeed, I put an example which can be useful I think:

Some context:
I have many requests triggered in background (they can arrive in a random order) and I need to mock only a specific one.

        // getting the extended info about the most recent request
        const lastReqInfo = MockAxios.lastReqGet();
        if (lastReqInfo && lastReqInfo.url.includes('/myApiThatIWantToTarget')) {
            mockAxios.mockError('You must fail ! \(ᵔᵕᵔ)/  ', lastReqInfo);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants