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

Regression in node axios interception / resolver in 0.38 #1119

Closed
tobiasr opened this issue Feb 24, 2022 · 4 comments
Closed

Regression in node axios interception / resolver in 0.38 #1119

tobiasr opened this issue Feb 24, 2022 · 4 comments
Labels
bug Something isn't working needs:reproduction

Comments

@tobiasr
Copy link

tobiasr commented Feb 24, 2022

Describe the bug

Using a very simple setup results in intercepted requests not resolving in 0.38 . The same code works fine in 0.36.8 .

Environment

  • msw: 0.38.0
  • nodejs: 16.14.0
  • npm: 8.3.1
  • axios: ^0.26.0

To Reproduce

The following test works on msw 0.36.8 but not in 0.38.0, where the intercepted request doesn't resolve which is reported as a jest async timeout "thrown: "Exceeded timeout of 5000 ms for a test.".

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

const handlers = [
  rest.get('http://localhost/the_endpoint', (req, res, ctx) => {
    return res(ctx.status(200));
  }),
];

const server = setupServer(...handlers);

// Enable API mocking before tests.
beforeAll(() => server.listen());

// Reset any runtime request handlers we may add during the tests.
afterEach(() => server.resetHandlers());

// Disable API mocking after the tests are done.
afterAll(() => server.close());

test('mock axios call and return', () => {
  return axios.get('http://localhost/the_endpoint').then(() => {
    expect(true).toBe(true);
  });
})

Expected behavior

Expected behaviour is that the test above passes.

@tobiasr tobiasr added the bug Something isn't working label Feb 24, 2022
@kettanaito
Copy link
Member

Hey, @tobiasr. Thanks for reporting this!

Unfortunately, I can't seem to reproduce the issue using your example above. I've also got some tests for axios.get() which are passing on the latest "interceptors" library.

Could you please create a reproduction repository where I can take a look at this? Thanks.

@textbook
Copy link

The example in the post also fails for me:

$  npm i msw@0.36  

added 4 packages, removed 1 package, changed 3 packages, and audited 1445 packages in 3s

157 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
$ npm t dummy   

> impasse@0.9.0 test
> jest "dummy"

 PASS   server  server/dummy.test.js
  ✓ mock axios call and return (9 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.264 s, estimated 6 s
Ran all test suites matching /dummy/i.
$ npm i msw@0.38

added 1 package, removed 4 packages, changed 3 packages, and audited 1442 packages in 1s

157 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
$ npm t dummy

> impasse@0.9.0 test
> jest "dummy"

 FAIL   server  server/dummy.test.js (5.283 s)
  ✕ mock axios call and return (5001 ms)

  ● mock axios call and return

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      20 | afterAll(() => server.close());
      21 |
    > 22 | test("mock axios call and return", () => {
         | ^
      23 |      return axios.get("http://localhost/the_endpoint").then(() => {
      24 |              expect(true).toBe(true);
      25 |      });

      at Object.<anonymous> (server/dummy.test.js:22:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        5.307 s
Ran all test suites matching /dummy/i.

This is with Node 16.14.0/NPM 8.3.1 on macOS 12.2.1.


I don't know if it's helpful/related, but I was looking at issues with 0.38 because the upgrade from 0.36.8 caused a bunch of my existing tests to fail by timing out - in that case, as well as the timeout, I also see:

    TypeError: Cannot read properties of undefined (reading 'content-encoding')

      at node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts:20:23
      at Object.getIncomingMessageBody (node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts:11:10)
      at NodeClientRequest.<anonymous> (node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/NodeClientRequest.ts:188:34)
      at step (node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:48:23)
      at Object.next (node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:29:53)
      at node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:23:71
      at Object.<anonymous>.__awaiter (node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:19:12)
      at NodeClientRequest.<anonymous> (node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/NodeClientRequest.ts:187:68)

@kettanaito
Copy link
Member

Oh, I see. This is an issue related to Node 16 moving the "headers" property of the IncomingMessage to its prototype. We're tracking it here: mswjs/interceptors#212. Once it's addressed on the interceptors' side it'll propagate to MSW.

@princefishthrower
Copy link

@kettanaito - that issue you mentioned (mswjs/interceptors#212) appears to be merged now into the interceptors library... is it safe to assume the fix has been incorporated in 0.38.2 ? Because I'm still seeing issues with axios.interceptors not being intercepted by msw...

@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:reproduction
Projects
None yet
Development

No branches or pull requests

4 participants