Angular4 Karma Wildcard

I have an api running on a server myserver.com:4567/api

and I want every request from my angular app to appear here when it matches / api / *. I already installed this with help proxy-conf.json

and it works great for maintenance. However, I want to test my app with karma and it doesn't seem to accept wildcards for proxy.

How can I solve this problem without providing karma for every possible endpoint.

I want it:

proxies: {
  "/api/*": "http://mybackend.com:4567"
}

      

instead of this:

proxies: {
  "/api/1": "http://mybackend.com:4567/api/1"
  "/api/2": "http://mybackend.com:4567/api/2"
  "/api/3": "http://mybackend.com:4567/api/3"
  "/api/4": "http://mybackend.com:4567/api/4"
}

      

+3


source to share


1 answer


As you can imagine, Karma does not offer wildcards for proxy entries. However, when we look at how proxying is done, you can spoof endpoints with a file per endpoint and with a single proxy entry:

proxies: { "/api": "/base/fake-api" }

Then you have a folder fake-api

with content, for example:

  • fake-api
    • some other route
      • different result-1-
      • different result-2-
    • 1
    • 2
    • 3
    • 4


Where I have subfolders and files containing results that I would like to fake from api. I did exactly this on a project to overcome the same problem. You will also need to write files, for example:

files:[ ... { pattern: "fake-api/**/*", included: false, served: true, watched: false }, ... ],

(You may have to tweak the path according to your desired location, for example I have a fake-api under my spec, so all the fake-api paths above are actually specs / fake-api in my project, but you get an image) ...

+1


source







All Articles