question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req doesn't seem to be correctly set when using node

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
Axnyffcommented, Jun 10, 2020

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

4reactions
kettanaitocommented, Jun 10, 2020

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.url is a URL instance, that is not serialized when printed into stdout. That’s why you see {}, which may falsely look like an empty object.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found