Angular ngrx test with jest throw error ` console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29`

563 Views Asked by At

I do the testing for my ngrx/store with jestjs. i got the testing passing result. eventhough getting an error like:

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Cross origin http://localhost forbidden
        at dispatchError (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:60:19)
        at Object.validCORSHeaders (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:72:5)
        at receiveResponse (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:842:21)
        at Request.<anonymous> (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:677:38)
        at Request.emit (events.js:203:13)
        at Request.onRequestResponse (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\request\request.js:1066:10)
        at ClientRequest.emit (events.js:203:13)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:575:27)
        at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17)
        at TLSSocket.socketOnData (_http_client.js:449:22) undefined

Any one help me to understand this above issue? and how to solve it?

Thanks in advance.

UPDATE: as per Luctia suggession i fixed the cors issue. but getting an error like:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

      at mapper (node_modules/jest-jasmine2/build/queueRunner.js:25:45)

need to add any workaround for this?

2

There are 2 best solutions below

0
timdeschryver On BEST ANSWER

Seems like you're using the store and effects for the test. I'm going to assume the effect will make an http request, causing this issue.

That's why I would encourage to mock the service:

{
  provide: GoogleBooksService,
  useValue: { searchBooks: jest.fn() },
}

See the NgRx Example App for an example.

1
Luctia On

It looks like your browser is blocking Cross-origin Read (CORB). Take a look at this article. Hope that helps!