Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using typescript, jest and http2:true causes RequestError: connect ECONNREFUSED 127.0.0.1:443 #1414

Closed
2 tasks done
ssypi opened this issue Aug 19, 2020 · 5 comments · Fixed by szmarczak/http2-wrapper#50
Closed
2 tasks done

Comments

@ssypi
Copy link

ssypi commented Aug 19, 2020

Describe the bug

http2:true option causes error when using got inside tests with jest and typescript.

  • Node.js version: 14.8.0
  • OS & version: macOS Catalina 10.15.4
  • got version 11.5.2

I saw some other ECONNREFUSED issues where the problem was old version of agent-base, but I have very simple list of dependencies and "npm ls agent-base" found nothing.

The same code works without jest so maybe it's not necessarily got's fault, but I'd like some help pinpointing the problem.

dependencies in the test repo:

    "@babel/core": "^7.11.1",
    "@babel/preset-env": "^7.11.0",
    "@babel/preset-typescript": "^7.10.4",
    "got": "^11.5.2",
    "jest": "^26.4.0",
    "typescript": "^3.9.7"

Possibly one of these causes the problem, all are the latest versions though.

Actual behavior

await got("https://google.com", {http2: true});
throws error
RequestError: connect ECONNREFUSED 127.0.0.1:443

Expected behavior

Should work. Works if http2 is false. Also works if running the typescript code without jest.

Code to reproduce

I have created a simple repo at https://github.com/ssypi/got-http2-jest

Just clone, npm install and run npm test.

There are two tests inside tests/test.ts, one with http2: true which errors and one without http2 which works.

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@xamgore
Copy link
Contributor

xamgore commented Aug 21, 2020

  • got uses http2wrapper package, auto mode to implement http2
  • got passes url to make a request
  • http2wrapper accepts url, checks it using instanceof URL, merges it with options
  • in pure js, non-typescript env:
    • instanceof URL returns true, so options.hostname = url.hostname
    • google.com is fetched
  • in Jest env:
    • Jest replaces the global URL with its own version (see the thread)
    • instanceof URL returns false, so options.hostname is undefined
    • options.hostname, options.host are undefined, so options.hostname == "localhost"
    • 127.0.0.1 is fetched and an exception is raised
    • when const { URL } = require('url') is added to http2wrapper to redefine global Jest's URL, test behaves as expected

@mwallace72
Copy link

Is szmarczak/http2-wrapper#50 with latest 11.8.0? I'm using that release and experiencing this.

@szmarczak
Copy link
Collaborator

Not yet. Anyway HTTP/2 is still buggy on Node.js' side, therefore I do not recommend running it on production.

@mwallace72
Copy link

Oh I'm sorry - I must have read this wrong, I was seeing this error message, but without using {http2: true}. My mistake.

@szmarczak
Copy link
Collaborator

Then I'm 100% sure there is an old version of agent-base in your dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants