1

I am encountering this CORS issue in my spec file in my angular test. Not sure how to fix this. The issue happens in the second line

beforeEach(() => {
    fixture = TestBed.createComponent(FhcComponent); //ISSUE HAPPENS HERE
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

Here is the full error I am seeing in console.

Access to XMLHttpRequest at 'ng:///FhcComponent/%C9%B5fac.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, chrome-untrusted, https.

Gani Lastra
  • 245
  • 1
  • 14
  • Have you tried what is proposed [here](https://stackoverflow.com/questions/45399079/angular-tests-failing-with-failed-to-execute-send-on-xmlhttprequest)? – Will Alexander Mar 07 '22 at 14:45
  • Yes. I have no Input() in my component. I have tried source-maps false, but doing this does not allow me to debug the spec file. There are other tests in this file that I need to fix(like TypeError: Cannot read properties of undefined (reading 'pipe')), which is caused by the issue with the TestBed.createComponent – Gani Lastra Mar 07 '22 at 15:25
  • Are you mocking everything in your test module, including any child components? – Will Alexander Mar 07 '22 at 15:27
  • @WillAlexander no. I have a bunch of ViewChild - TemplateRef, and a ViewChilden . Should I mock them? – Gani Lastra Mar 07 '22 at 15:37
  • ViewChildren themselves don't need to be mocked. Does your component display any other components? Does it inject any services? What error is shown when you add the source maps flag? – Will Alexander Mar 07 '22 at 15:50
  • It does display another component (I see 'ap-select-autocomplete' child component). I see these in my constructor -> private router: Router, private someService: SomeService, private someMoreService: SomeMoreService, public dialog: MatDialog – Gani Lastra Mar 07 '22 at 15:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242687/discussion-between-gani-lastra-and-will-alexander). – Gani Lastra Mar 07 '22 at 16:00

0 Answers0