msw: Axios never receives mocked response object msw@0.38.1
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

Current behavior
Test fails with msw@0.38.1 and node v17.1.0
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 7
- Comments: 26 (11 by maintainers)
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, justnpm install
dependencies again.I’m so thankful to everybody for providing reproduction repositories and sandboxes! That made debugging and verification of this issue such a breeze.
Going from
^0.39.2
back to0.36.8
finally got MSW to work with POST request - was working fine with GET request but POST just was not intercepted or ever ended.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
I found a similar problem with
post
rest handlers.Take a look on that:
Tested with
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:
msw@0.36.8
node: v16.14.0
axios@0.26.0
@95th has done some outstanding work to fix this in https://github.com/mswjs/interceptors/issues/259. We’ve merged the fixed, it should propagate to MSW in the next dependency bump.
So, in the end, no changes on the
follow-redirects
side were necessary. I still find it somewhat odd the way they call.end()
within the.write()
callback. It’s not prohibited per se but unusual from the usage examples of Node requests I’ve seen. We’ve circumvented this by rewriting the way Interceptors will call write callbacks.I’ve opened a pull request in the
follow-redirects
repository to address this behavior change (https://github.com/follow-redirects/follow-redirects/pull/197). I hope the maintainers will be okay with it and we can merge and propagate it to all the immediate dependencies in the nearest future.I can also confirm that the proposed change fixes the issue!
More insight
Creating requests with
axios
establishes the following dependency tree:Axios calls the
req.end()
, wherereq
is instance ofRedirectableRequest
. TheRedirectableRequest
, however, calls the.end()
on the originalhttp.request
(ClientRequest
) instance in the.write()
callback:Now the root cause of the issue:
NodeClientRequest
only collects body chunks upon.write()
calls. It then does the actual writing (and callback execution) as a part of the.end()
method:https://github.com/mswjs/interceptors/blob/219f9f3cd82f5baf3297f7c6095caa356c148d31/src/interceptors/ClientRequest/NodeClientRequest.ts#L170-L179
And since
RedirectableRequest
executes.end
in the callback of.write
, theNodeClientRequest.end
never gets called at all. This causes the request instance to pend, reaching the test’s timeout.@kettanaito Here you go: https://github.com/sbrandwoo/msw-timeout-example
Tests pass with MSW 0.36.8.
Hey, @Nnadozie. Thanks for reporting this.
I can confirm that the reproduction repository has tests passing on
0.38.0
and Node.js14.17.0
. This appears to be an issue related to Node.js versions. We’ve received quite a few recently and those may even be related.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
16.13.0
I can see some exceptions:The same errors happen on
17.1.0
.We should update MSW and its dependencies to support Node.js 16, I think this will solve the issue with version 17 as well.
You are right, I can confirm my tests work with
msw@0.36.8
👍@alebrozzo did you ever find a workaround? We are hitting the same issue
Hello, I wanted to mention we started getting this error with the latest update of axios. We are using axios v1.4, react-query v4.32.6, msw (for tests) v1.2.5, and node v16.15.0. Our renovate bot has created a new merge request to update axios to v1.5 and a few of our tests fail. Looking into them the problem is when mocking a call to a post request that has a data parameter, similar to what @teklakct mentioned in this example. If I remove the data parameter from the real call, then the tests pass.
I updated msw to v1.3 but the same thing happens.
Also waiting for a possible workaround. Meanwhile, going back to 0.36.8?
🎉 This issue has been resolved in version 0.39.0 🎉
The release is available on:
Your semantic-release bot 📦🚀