Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting CORS error when running Jasmine and Karma in Angular

I am running specs and notice the following error in the browser window

zone.js:2990 Access to XMLHttpRequest at 'ng:///DynamicTestModule/NewPracticeQuestionComponent_Host.ngfactory.js' from origin 'http://localhost:9876' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

I suppose somewhere in my application, some message has been sent out which is causing CORS issue. I assume its the web server used by Karma which is blocking the request.

Could I configure Karma to disable CORS policy?

like image 933
Manu Chadha Avatar asked Jan 08 '19 17:01

Manu Chadha


People also ask

How do you fix the CORS issue in angular application?

We call this the CORS error. CORS error due to browser's same origin policy. To get around this, you need to tell your browser to enable your client and your server to share resources while being of different origins. In other words, you need to enable cross-origin resource sharing or CORS in your application.

How do I stop CORS errors?

To get rid of a CORS error, you can download a browser extension like CORS Unblock. The extension appends Access-Control-Allow-Origin: * to every HTTP response when it is enabled. It can also add custom Access-Control-Allow-Origin and Access-Control-Allow-Methods headers to the responses.

What is the role of Jasmine and karma in angular testing?

Karma handles the process of creating HTML files, opening browsers and running tests and returning the results of those tests to the command line. If you use the Angular CLI to manage projects it automatically creates stub Jasmine spec files for you when generating code.


1 Answers

Run your test with --sourcemaps=false and you will get the right error messages.

This is an issue here : https://github.com/angular/angular-cli/issues/7296

Also, Shorthand :

ng test -sm=false

As of angular 6 the command is:

ng test --source-map=false
like image 113
Atif Qadri Avatar answered Oct 22 '22 23:10

Atif Qadri