Axios never receives mocked response object msw@0.38.1
See original GitHub issueDescribe 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:23 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.