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.

Allow for proxying real http calls to backends

See original GitHub issue

🚀 Feature Proposal

It would be nice if Jest allowed for proxying real http requests sent to backends from tests.

Motivation

There are many use cases to sending real http requests to backends from jest. Take for instance contract testing with Pact-js. One needs to issue a real request from jest to a mock backend in order to generate the contract/pact. It would be nice if one could configure http proxying by easily changing the default port and url (see example below).

Example

Say a client need to issue a http POST to the backend as follows:

createUser(user: User): Observable<User> {
    return this.httpClient.post<User>('/api/user', user);
}

By default it will issue a call to: http://localhost:80/api/user when executed from a jest test (the test is not shown here). I would like instead to call the following URL: http://other_host:8080/api/user (or any other URL).

As of now one needs to modify the actual client by passing the URL and the port as in this example from Pact-js. This forces the test implementer to change the production code for testing purposes which is bad.

I know Karma (see documentation) offers a proxying option as follows:

    proxies: {
      '/api': 'http://other_host:8080',
    },

I wish this were possible with my favorite js testing framework, Jest. Please share your thoughts and feedback.

P.S: Please bear in mind this feature is not about mocking http requests but issuing real http requests from Jest.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
SimenBcommented, Sep 8, 2019

Another possibility is to keep the urls in a constants.js (or whatever name) file, and mock it using jest.mock in your test to return a different url. Keeping urls as constants seems like a good refactor anyways

0reactions
github-actions[bot]commented, May 11, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is an API Proxy?
An API proxy is an interface that sits between your frontend and the actual backend services. Using an API proxy decouples the frontend...
Read more >
Proxy HTTPS requests to a HTTP backend with NGINX
The type of proxy you are trying to set up is called a reverse proxy. A quick search for reverse proxy nginx got...
Read more >
Reverse Proxy Guide - Apache HTTP Server Version 2.4
Simple reverse proxying ... In the above, any requests which start with the /images path with be proxied to the specified backend, otherwise...
Read more >
Proxying API Requests in Development
The proxy option supports HTTP, HTTPS and WebSocket connections. If the proxy option is not flexible enough for you, alternatively you can: ...
Read more >
React — How To Proxy To Backend Server
Explaining how to configure a proxy for backend API calls with an example. ... Let's start the server with this command npm run...
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