Repository navigation
urlRoot in karma.conf.js can't be set to anything other than the default /聽#22132
Description
Activity
- changed the title
[-]`urlRoot` in `karma.conf.js` can't be changed to anything other than the default `/`[/-][+]`urlRoot` in `karma.conf.js` can't be set to anything other than the default `/`[/+]on Nov 10, 2021 Hi @colossatr0n,
Can you please share the use-case of why do you need to set a custom urlRoot?
- addedneeds: more infoReporter must clarify the issueReporter must clarify the issue
on Nov 10, 2021 Yes, I'll try my best to explain it.
TL;DR: Being able to specify
urlRootallows for pact testing Angular service requests that can't otherwise be tested when the application's base URL is set by Tomcat.I'm currently using the
karma-pactplugin to run pact tests throughkarma. The way the application is set up is like this:angular-cliis used to compile the UI.- Tomcat is used to serve the contents of the dist directory.
- Tomcat sets the application's base URL from
/to/someBaseUrl.
In the Angular services, I have to make sure that all request URLs don't start with
/:// When the application is run through Tomcat, this URL becomes `/someBaseUrl/request/url` <--- correct this.http.get<any>("request/url") // If the URL is incorrectly prepended with `/`, this URL becomes `/request/url` <--- incorrect this.http.get<any>("/request/url")
When testing locally using
ng serve, using either URL works since both get proxied to/someBaseUrl/request/url. This isn't the case when running the application through Tomcat since there's no proxy involved.A pact test is able to catch an error like this, but with the way the application is architected, it can't. The reasons why this doesn't work are:
- Pact test requests are made from the karma server
localhost:9876 - Regardless of if the Angular service prepends the request URL with
/, the result is the same:
localhost:9876 + request/url = localhost:9876/request/url localhost:9876 + /request/url = localhost:9876/request/url <--- pact test won't detect that the request was prepended with `/`To the pact test, everything looks fine, but when the application is deployed and tested, any requests prepended with
/will result in 404s.Being able to specify a base for the karma server would allow the pact tests to properly detect the difference:
localhost:9876/someBaseUrl + request/url = localhost:9876/someBaseUrl/request/url localhost:9876/someBaseUrl + /request/url = localhost:9876/request/url <--- pact test will detect this as a failureHi @colossatr0n,
Thanks for the above explanation.
I am trying to understand a bit better your test setup. When running
ng test, the application is served from memory similar tong serve. Therefore, I am a bit confused by what you mean you are serving the contents of thedistdirectory using Tomcat for testing.In your
karma.conf.jsyou can easily setup a proxy to redirect your HTTP requests.Example:
module.exports = (config) { config.set({ // ... other configurations proxies: { 'request/': 'http://127.0.0.1:1234/someBaseUrl/request/' // NB: this target is your pact server. } }); };
For more information about proxies in Karma see https://karma-runner.github.io/6.3/config/configuration-file.html#proxies.
Sorry, the explanation for serving the contents of the
distfolder was just to show how the application is served. Tomcat isn't used for running the tests.The issue with using the proxy with a
urlRootof/is that a request made to/requestorrequestresults in a successful proxied request and I need/requestto not be proxied. If I could set theurlRoot, I could use the proxy like so:module.exports = (config) { config.set({ // ... other configurations proxies: { '/someBaseUrl/request/': 'http://127.0.0.1:1234/someBaseUrl/request/' } }); };
With a
ulrRootofsomeBaseUrl, a request to/request(localhost:9876/request) wouldn't get proxied, while a request torequest(localhost:9876/someBaseUrl/request) would get proxied.- addedfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyand removedneeds: more infoReporter must clarify the issueReporter must clarify the issue
on Nov 22, 2021
馃悶 Bug report
Command (mark with an
x)Is this a regression?
No.
Description
Can't set
urlRootinkarma.conf.jsto anything other than "/" because:which is described in detail in this issue: karma-runner/karma#3357 (comment).
馃敩 Minimal Reproduction
Run
ng testwith akarma.conf.jsthat contains aurlRootset to anything but/:馃敟 Exception or Error
The error that is logged is the following:
馃實 Your Environment