-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Unable to be used alongside superagent #209
Comments
Hey, @boblauer. Thanks for reaching out. I can see that, although you're calling For example, if you wish to intercept a import { rest } from 'msw'
import { setupServer } from 'msw'
setupServer(
// The `rest.get` call is a request handler
rest.get('http://localhost:8080/', (req, res, ctx) => {
return res(ctx.json({ intercepted: 'sure' }))
}
).listen() Notice how I'm giving an absolute URL to
MSW is agnostic of your server framework, so if you wish it to intercept a request to your app, provide an absolute URL that you wish to intercept. In the example above I've assumed that the Also, from your original task I got the impression that you'd like to mock a third-party request, not the request to your app. Perhaps, you should consider: setupServer(
// Intercepting a third-party request
rest.get('https://third-party.address/route', (req, res, ctx) => {...})
).listen()
// Request to a third-party server
request('https://third-party.address/route') Let me know if that helps. |
Thanks for the response. My example was more simplistic than my real world code. In my real world code, I was intercepting the third-party request made from within my express controller, but what I don't want to intercept is the call to my express controller. So my code looked a lot like your last example, except my request to a third-party server is happening with an express controller. However, regardless of what I pass to |
I've set up a reproduction repository and was able to get the same issue. Some of the |
Technical detailsThe issue is caused by the I'm going to provide a fix in the |
The fix is published in |
I am having this issue in version 0.19.5 while trying to test my GraphQL app. The GraphQL resolver within the app makes an external API call to a 3rd party to get a list of websites. The mocking of that call using Here is my test file: import supertest from 'supertest';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
// app is my ApolloServer
import { app } from '../app';
import { getWebsites } from './gql';
const server = setupServer(
rest.get(`https://the-third-party-api.com/created`, (_req, res, ctx) => {
return res(ctx.json([`fake-site-1`, `fake-site-2`]));
})
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test(`Get all websites`, async () => {
await supertest(app)
.post(`/`)
.send({ query: getWebsites })
.then(({ error }) => {
console.log(error);
expect(error).toBeFalsy();
});
}); Here is the output when I run the test:
If I remove the msw Am I doing something wrong? |
Hey, @bopfer. Sorry to hear that. Could you please let me know the exact versions of these dependencies that you are using? $ npm ls msw
$ npm ls node-request-interceptor |
The packages seem to be up to date, thanks. One thing I can notice is that you mock a |
The |
Something else must be at place then. Could you please set up a minimum reproduction repository for us to look? That'd be most useful. |
Will do! This is sort of a side project. So, it may take me a few days to put something together. |
Here is the reproduction repo: https://github.com/bopfer/msw-supertest-issues I tried to make it as slim as possible to show the issue. |
@kettanaito is there a fix for this issue and even i am getting the same problem. |
@manishiitg, hey. The fix for this issue has been provided. The issue you are experiencing may be similar, but not the same. Thanks for opening an issue regarding your problem, I'll look into that. |
@bopfer, thanks for making a reproduction repository, this is most useful! I understand some of the issue also relate to |
Describe the bug
I have an express server that makes calls to third party apis, and I would like to test my express server using supertest and mock the third party api requests using msw.
When starting the msw node server, all supertest requests fail with the following error:
Environment
msw: 0.19.0
nodejs: 10.16.3
npm: 6.9.0
To Reproduce
Steps to reproduce the behavior:
The text was updated successfully, but these errors were encountered: