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.

Unable to be used alongside superagent

See original GitHub issue

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:

TypeError [ERR_INVALID_URL]: Invalid URL: undefined//undefined
      at onParseError (internal/url.js:241:17)
      at new URL (internal/url.js:319:5)
      at new URL (internal/url.js:316:22)
      at normalizeHttpRequestParams (node_modules/node-request-interceptor/lib/http/ClientRequest/normalizeHttpRequestParams.js:29:57)
      at new ClientRequestOverride (node_modules/node-request-interceptor/lib/http/ClientRequest/ClientRequestOverride.js:65:74)
      at handleRequest (node_modules/node-request-interceptor/lib/http/override.js:28:12)
      at Object.requestOverride [as request] (node_modules/node-request-interceptor/lib/http/override.js:55:20)
      at Test.Request.request (node_modules/superagent/lib/node/index.js:622:31)
      at Test.Request.end (node_modules/superagent/lib/node/index.js:764:8)
      at Test.end (node_modules/supertest/lib/test.js:125:7)

Environment

  • msw: 0.19.0
  • nodejs: 10.16.3
  • npm: 6.9.0

To Reproduce

Steps to reproduce the behavior:

import express from 'express';
import request from 'supertest';
import { setupServer } from 'msw/node';

const app = express();
app.get('/', (req, res) => res.send('hello world'));

setupServer().listen();

request(app)
  .get('/')
  .then((res) => {
    console.log(res);
  });

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
kettanaitocommented, Jun 13, 2020

Technical details

The issue is caused by the RequestOptions object supertest creates. Unlike regular NodeJS RequestOptions, the one from the mentioned library doesn’t have the protocol and hostname property, which resulted into the derived url being undefined//undefined.

I’m going to provide a fix in the node-request-interceptor library (responsible for requests interception and mocking in Node) and propagate it to MSW.

1reaction
bopfercommented, Jul 6, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

superagent - npm
Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features.
Read more >
Unable to post file to server using superagent - Stack Overflow
For file upload, I use a library called Dropzone. To send the file over, I am using a library called superagent.
Read more >
superagent - npm.io
superagent -declare - A simple declarative API for SuperAgent. superagent-node-http-timings - measure http timings in node. js.
Read more >
5 ways to make HTTP requests in Node.js - LogRocket Blog
Make HTTP requests in Node.js using the native module as well as npm packages like Axios, Got, SuperAgent, and node-fetch.
Read more >
How to use the superagent.get function in superagent - Snyk
To help you get started, we've selected a few superagent examples, based on popular ways it is used in public projects. Secure your...
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