req doesn't seem to be correctly set when using node
See original GitHub issueDescribe the bug
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
Environment
msw: 0.19.0
nodejs: 12.16.1
npm: 6.13.4
To Reproduce
Here’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.
Expected behavior
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!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Why won't req.session set with node.js - Stack Overflow
Good point. I was just assuming it was run under the correct environment. Definitively check if the environment is set correctly. – NilsH....
Read more >Top 10 Most Common Node.js Developer Mistakes - Toptal
Mistake #4: Expecting Callbacks to Run Synchronously. Asynchronous programming with callbacks may not be something unique to JavaScript and Node.js, but they ...
Read more >15 Common Error Codes in Node.js and How to Fix Them
The ETIMEDOUT error is thrown by the Node.js runtime when a connection or HTTP request is not closed properly after some time.
Read more >Anatomy of an HTTP Transaction | Node.js
When an HTTP request hits the server, node calls the request handler function with a few handy objects for dealing with the transaction,...
Read more >Understanding Cookies and Implementing them in Node.js
Let's dive in and see how we can implement cookies using Node.js. ... //set a simple for homepage route app.get('/', (req, ...
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
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
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 inreq.url.searchParams
using a much more comfortableURLSearchParams
interface.