the first parameters in rest methods does not seem to be correctly set, at least req.url, req.query ( undefined) and req.params: empty object
msw: 0.19.0nodejs: 12.16.1npm: 6.13.4Here's a test to reproduce. I'd expect query to be defined but the value is undefined, there's no way for me to have the value of email in my handler.
import fetch from 'isomorphic-fetch';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer(
rest.get('/foo', (req, res, ctx) => {
console.log(req);
return res(ctx.text(''));
}),
);
describe('', () => {
beforeEach(() => {
server.listen();
});
afterAll(() => {
server.close();
});
it('should work', async () => {
const res = await fetch('http://localhost/foo?email=ok');
expect(await res.text()).toBe('');
});
});
The result of my console.log is
{
url: URL {},
method: 'GET',
body: '',
headers: Headers {
map: {
'accept-encoding': 'gzip,deflate',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
connection: 'close',
accept: '*/*'
}
},
params: {},
redirect: 'manual',
referrer: '',
keepalive: false,
cache: 'default',
mode: 'cors',
referrerPolicy: 'no-referrer',
integrity: '',
destination: 'document',
bodyUsed: false,
credentials: 'same-origin'
}
url seems suspect: empty, query is not defined and params is empty.
i'd expect req.url to have the full path of the request, req.query to be an URLSearchParams. Not sure what should be req.params
I'd be willing to help to fix this, if this a bug / missing feature.
Thanks for your help!
Hey, @Axnyff. Thanks for reporting this!
Please, can you try req.url.searchParams.get('email')?
The query parameters references via req.query were deprecated, as it effectively duplicates what you can access in req.url.searchParams using a much more comfortable URLSearchParams interface.
Note that
req.urlis aURLinstance, that is not serialized when printed into stdout. That's why you see{}, which may falsely look like an empty object.
This indeed works! my bad
I almost got fooled again as req.url.searchParams also prints something that looks empty, really weird...
Thanks for your help anyway!
This page probably needs to be updated: https://redd.gitbook.io/msw/recipes/query-parameters
I can look into this if the docs are hosted on github
Arf, It's actually not the website sources that is hosted on github so I'm not sure what I can to do to help
Came here with the same problem.
The above solution does work.
However, I suggest correcting the docs as that is the first place people look answers. Most tutorials all over the internet also follow the old procedure.
Thanks for verifying that!
I've updated the "Query parameters" page in the current docs.
My bad about the misleading documentation. I'm currently working hard on the custom website and completely revamped docs, which I'm sure you will love. Keeping two versions of docs effectively leads to things being overlooked.
Most helpful comment
This indeed works! my bad
I almost got fooled again as req.url.searchParams also prints something that looks empty, really weird...
Thanks for your help anyway!
This page probably needs to be updated: https://redd.gitbook.io/msw/recipes/query-parameters
I can look into this if the docs are hosted on github