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.

Axios never receives response object

See original GitHub issue

Looking to integrate msw into our testing, using serverSetup in our jest tests, and feel like I must be missing something simple. I’ve setup my handlers, and can log my request coming into the resolve. However, that response never seems to make it to Axios, so the mock response never makes it to my interface. No response is received, no error is thrown, just…nothing

Environment

  • msw: 0.17.0
  • nodejs: 10.14.0
  • npm: 6.13.4
  • axios: 0.19.2

I’m running these tests via jest in a VS Code Terminal on a 2019 MacBook Pro (latest VS Code)

To Reproduce

I have setup a super simple test

import { rest } from 'msw';
import { setupServer } from 'msw/node';
import axios from 'axios';

describe('Axios test', () => {
  const handlers = [
    rest.get('/health/PeopleManagement/api/Person', (req, res, ctx) => {
      console.log('in request');
      return res(
        ctx.json({
          total: 0,
          data: [],
          count: 0,
        })
      );
    }),
  ];
  const server = setupServer(...handlers);

  beforeAll(() => server.listen());
  afterAll(() => server.close());

  it('should make a call for data', async () => {
    try {
      const types = await axios.get('/health/PeopleManagement/api/Person');
      console.log('types ', types);
    } catch (err) {
      console.error(err);
    }
  });
});

Expected behavior

You expect to see the console.log from the handler, that the call would resolve, and that types would now be the simple object and output to the console (or at least an error in the catch)

Current behavior

Instead, what you see is the console.log from the handler (we made it to the handler), followed by this error:

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error

Axios

Under the hood, axios (in a node execution context like this) uses either http or https for it’s transport, depending on the url protocol involved. It does this via this adapter.

Final Bit

Not sure what I’m doing wrong here. Do I require a custom request handler of some sort? Custom context? Been hammering away for a few hours now, and just getting nowhere…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
venturalpcommented, Jul 3, 2020

Hey guys, I’m facing the same problem, any news about it?

1reaction
AustinNChristensencommented, Jun 12, 2020

Hi all, I am experiencing this same issue with msw@0.19.0, but it works when I revert to msw@0.17.1. It seems that there may be a regression of this bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise Never Resolves Using Axios - Stack Overflow
Unfortunately, I am unable to get axios to return any valid data on requests. In most cases when we await a response, the...
Read more >
Handling Errors With Axios - Stack Abuse
Axios is a JavaScript library that uses the Promise API to create HTTP ... never received a response, and the request was never...
Read more >
Understanding Axios POST requests - LogRocket Blog
Learn how to use the Axios POST method in both vanilla JavaScript and in a framework like React to send requests.
Read more >
Getting Started With Axios: A Popular Promise-Based HTTP ...
When all the requests get fulfilled, it will produce a single promise which will contain the response objects for each request. To get...
Read more >
axios error 500 bad response - You.com | The AI Search ...
axios.get('/user/12345') .catch(function (error) { if (error.response) { // The ... in state and update it accordingly on the response object you received.
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