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:
- Created 4 years ago
- Comments:6
Top GitHub Comments
Another possibility is to keep the urls in a
constants.js
(or whatever name) file, and mock it usingjest.mock
in your test to return a different url. Keeping urls as constants seems like a good refactor anywaysThis 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.