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 mocked response object msw@0.38.1

See original GitHub issue

Describe the bug

I have set up a simple interceptor, and can log my mocked response using the lifecycle event 'response:mocked', however my mocked response is never received by my axios get request.

After reading through #180 and trying out msw@0.17.1, the issue is resolved, so my initial thought is that this is likely a regression. However, when recreating the issue in codesandbox I notice the issue is resolved with msw@0.38.1 and node v14.18.1

Environment

  • msw: 0.38.1
  • nodejs: 17.1.0
  • npm: 8.1.2
  • axios: 0.26.0

Please also provide your browser version. This is a node environment.

To Reproduce

I have created a very simple test in a similar nestjs environment to the one I use. Codesandbox link

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

describe('AppController', () => {
  let server: SetupServerApi;
  beforeAll(async () => {
    server = setupServer(
      rest.get('https://contest.com/', (req, res, ctx) => {
        return res(
          ctx.json({
            message: 'got some data',
          }),
        );
      }),
    );
    server.listen();
  });

  afterAll(async () => {
    server.close();
  });

  describe('reproduce', () => {
    it('should get mocked response', async () => {
      try {
        const res = await axios.get('https://contest.com/');
        console.log(res);
        expect(res.data.message).toBe('got some data');
      } catch (error) {
        console.error(error);
      }
    });
  });
});

Expected behavior

Expect to see response object with a passing test.

Test passes with msw@0.38.1 and node v14.18.1

image

Current behavior

Test fails with msw@0.38.1 and node v17.1.0 image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

8reactions
kettanaitocommented, Jun 9, 2022

The fix should propagate automatically in @mswjs/interceptors@0.16.5. The package will be released tonight. No updates on the MSW side should be necessary, just npm install dependencies again.

7reactions
kettanaitocommented, Apr 20, 2022

I’m so thankful to everybody for providing reproduction repositories and sandboxes! That made debugging and verification of this issue such a breeze.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test axios get request function in jest/enzyme tests?
Have you tried mocking promise as below: var mockPromise = new Promise((resolve, reject) => { resolve(<mock response similar to actual ...
Read more >
Mocking Axios in Jest + Testing Async Functions | Leigh Halliday
We will be using Jest and some mocking functionality that it provides. ... "mock axios" will be an object with 1 property called...
Read more >
Mocking Axios in Jest + Testing Async Functions - YouTube
Article and source code here: https://www.leighhalliday.com/ mocking - axios -in-jest-testing-async-functionsIn this video we'll look at a ...
Read more >
How to Emulate Axios/Fetch to Handle HTTP Requests in ...
As we can see in the code, we created a file called server.js inside a folder called mocks , in that file we...
Read more >
@openagenda/axios-mock-adapter - npm
Start using @openagenda/axios-mock-adapter in your project by running ... Mock any GET request to /users // arguments for reply are (status, ...
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