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 never receives mocked response object msw@0.38.1 #1125
Comments
I'm seeing the same using node v16.13.1 (tested on v16.14.0 too), axios v0.26.0 and msw v0.38.1. Handlers are called but Axios never receives a response. EDIT - Here is a reproduction: https://github.com/innocenzi/vitest-msw-issue-1125
|
Thanks for sharing @innocenzi. Your comment made me look into testing node LTS v16.14.0 with different msw versions, and it seems to work with:
|
You are right, I can confirm my tests work with |
Hey, @Nnadozie. Thanks for reporting this. I can confirm that the reproduction repository has tests passing on MSW does not officially support any Node.js version higher than v12. It seems that there were some breaking changes in Node.js 16-17 that affected how we intercept requests. When running the tests on http GET http://localhost/fetch error when cloning response: TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at createHeadersLenient (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/node-fetch/lib/index.js:1021:28)
at NodeClientRequest.<anonymous> (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/node-fetch/lib/index.js:1498:20)
at NodeClientRequest.emit (node:events:390:28)
at NodeClientRequest.emit (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:286:46)
at NodeClientRequest.respondWith (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:360:14)
at NodeClientRequest.<anonymous> (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:188:34)
at step (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:48:23)
at Object.next (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:29:53)
at fulfilled (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:20:58) +0ms http GET http://localhost/axios error when cloning response: TypeError: Cannot read properties of undefined (reading 'location')
at RedirectableRequest._processResponse (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/follow-redirects/index.js:341:35)
at NodeClientRequest.RedirectableRequest._onNativeResponse (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/follow-redirects/index.js:57:10)
at Object.onceWrapper (node:events:510:26)
at NodeClientRequest.emit (node:events:390:28)
at NodeClientRequest.emit (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:286:46)
at NodeClientRequest.respondWith (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:360:14)
at NodeClientRequest.<anonymous> (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:188:34)
at step (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:48:23)
at Object.next (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:29:53)
at fulfilled (/Users/kettanaito/Projects/contrib/msw-1125/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js:20:58) +0ms The same errors happen on We should update MSW and its dependencies to support Node.js 16, I think this will solve the issue with version 17 as well. |
I found a similar problem with Take a look on that: import axios from 'axios';
import {rest} from 'msw';
import {setupServer} from 'msw/node';
const URL = 'http://mock.pl';
const MOCKED_RESPONSE = {title: 'Mocked response'};
const client = axios.create();
const server = setupServer(rest.post(URL, (req, res, ctx) => res(ctx.json(MOCKED_RESPONSE))));
beforeAll(() => server.listen());
afterAll(() => server.close());
it('works because post data is not defined', async () => {
const response = await client.post(URL);
expect(response.data).toEqual(MOCKED_RESPONSE);
});
it('does not work because there is a post data', async () => {
const response = await client.post(URL, {});
expect(response.data).toEqual(MOCKED_RESPONSE);
}); Tested with
|
🎉 This issue has been resolved in version 0.39.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@kettanaito I'm still seeing this issue with MSW 0.39.1. @teklakct's reproduction above still fails with a timeout. |
@sbrandwoo, please create a GitHub repo or Codesandbox where I can look into that. Thanks. |
@kettanaito Here you go: https://github.com/sbrandwoo/msw-timeout-example
Tests pass with MSW 0.36.8. |
Thanks for the repository, @sbrandwoo. I can confirm I can reproduce the issue locally. Will update this thread with more info once I have it. |
InsightThe issue manifest from the connection to Error: socket hang up
at connResetException (node:internal/errors:691:14)
at Socket.socketOnEnd (node:_http_client:466:23)
at Socket.emit (node:events:532:35)
at endReadableNT (node:internal/streams/readable:1346:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
} The interceptors catch this error but cannot replay it because the 2022-03-10T14:42:42.509Z http normalizeWriteArgs normalizing ClientRequest.write arguments... [ <Buffer 7b 7d>, undefined, [Function (anonymous)] ]
2022-03-10T14:42:42.509Z http normalizeWriteArgs successfully normalized ClientRequest.write arguments: [ <Buffer 7b 7d>, undefined, [Function (anonymous)] ]
2022-03-10T14:42:42.509Z http POST http://mock.pl/ write: {
chunk: <Buffer 7b 7d>,
encoding: undefined,
callback: [Function (anonymous)]
}
2022-03-10T14:42:42.509Z http POST http://mock.pl/ chunk successfully stored! [ <Buffer 7b 7d> ]
2022-03-10T14:42:42.510Z http POST http://mock.pl/ event:socket
# SHOULD BE CALLED HERE
2022-03-10T14:42:47.507Z http override done, restoring modules... Looks like |
More insightCreating requests with
Axios calls the this.write(data, encoding, function shouldEndAfter() {
console.log('write callback: calling currentRequest.end()...')
self._ended = true;
currentRequest.end(null, null, callback);
}); Now the root cause of the issue: And since |
Hi @kettanaito, thank you for looking into this issue. It is currently a blocker for my project, would you know a workaround to get the interceptors returning the mocks ? |
Also waiting for a possible workaround. Meanwhile, going back to 0.36.8? |
Going from |
Is there any other solution to the same problem besides downgrading to 0.36.8 ? |
Here's my proposal on how to resolve this issue: follow-redirects/follow-redirects#195. You can help make it happen with your contributions. I don't see a way to address it on MSW's side (see technical insights above). |
I've opened a pull request in the I can also confirm that the proposed change fixes the issue! |
I'm so thankful to everybody for providing reproduction repositories and sandboxes! That made debugging and verification of this issue such a breeze. |
@95th has done some outstanding work to fix this in mswjs/interceptors#259. We've merged the fixed, it should propagate to MSW in the next dependency bump. So, in the end, no changes on the |
The fix should propagate automatically in |
I still face the issue. Just this week I wrote the unit tests from scratch. And facing this issue. I'm mocking HTTP POST btw. Environment |
please disregard my post earlier. I was doing something wrong in my code, due to which, the interceptors were not being registered for the tests. Its resolved now. My bad. Apologies for the bother caused :( |
@ashish-g-lahane, could you please ensure you're using the latest version of MSW? If that still occurs, please create a reproduction repository and share it. |
Hello, I wanted to mention we started getting this error with the latest update of axios. I updated msw to v1.3 but the same thing happens. |
@alebrozzo did you ever find a workaround? We are hitting the same 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
To Reproduce
I have created a very simple test in a similar nestjs environment to the one I use. Codesandbox link
Expected behavior
Expect to see response object with a passing test.
Current behavior
The text was updated successfully, but these errors were encountered: