Unable to be used alongside superagent
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:3
- Comments:16 (9 by maintainers)
Top 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 >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
Technical details
The issue is caused by the
RequestOptions
objectsupertest
creates. Unlike regular NodeJSRequestOptions
, the one from the mentioned library doesn’t have theprotocol
andhostname
property, which resulted into the derived url beingundefined//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.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.