question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to mock reject reponse with status > 400

See original GitHub issue

How to mock reject reponse with status > 400 . I want to mock errorneous response i.g.

      moxios.wait(() => {
        const request = moxios.requests.mostRecent();
        request.respondWith({
          status: 422,
          response: { message: 'problem' },
        });
      });

How can I do it?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

15reactions
Sutikshancommented, Aug 12, 2016

I manage to do it via following :-

      const errResp = {
        status: 422,
        response: { message: 'problem' },
      };
      moxios.wait(() => {
        const request = moxios.requests.mostRecent();
        request.reject(errResp);
      });

Thanks for this lib.

5reactions
rickhanloniicommented, Sep 14, 2016

For anyone using stubRequest you should be able to just use a non 2XX status:

moxios.stubRequest(/.*/, {
  status: 400,
  response: { message: 'problem' }
});

axios.get('something')
  .then(() => {
    // Not hit
  })
  .catch(error => {
    // Error
  });

@Sutikshan it looks like respondWith does this as well. Here it calls axois.settle which as you can see here rejects for invalid response statuses. Not sure why it wasn’t working for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SuperTest / JEST , how to force or mock a 400 status code?
Mock find method of Thought model to reject with an error in your test. First, import the model in your test and automock...
Read more >
Mocking error responses - Recipes - Mock Service Worker Docs
Here's an example on how to mock an error response for the login POST request: ... 8 // Send a valid HTTP status...
Read more >
How to mock requests for unit testing in Node - freeCodeCamp
If you really need to test HTTP-specific code, and the response from the external API is relatively simple, use Nock, and manually mock...
Read more >
How to Mock API Calls in Test Environments - Stoplight Blog
Returns a 200 status with a users posts when requested · Request: {method:'get',body:{user:“Jack”}} · Response: {status:200,posts:posts:[“I just ...
Read more >
Successfully Throwing Async Errors with the Jest Testing Library
There's only one situation developers want errors to happen, and that's in specific tests ... Next, I tried to mock a rejected value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found