Angular ngrx 测试,开玩笑抛出错误`console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29`
Angular ngrx test with jest throw error ` console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29`
我用 jestjs
为我的 ngrx/store
做测试。我得到了测试通过的结果。尽管出现如下错误:
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Cross origin http://localhost forbidden
at dispatchError (C:2333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:60:19)
at Object.validCORSHeaders (C:2333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:72:5)
at receiveResponse (C:2333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:842:21)
at Request.<anonymous> (C:2333\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:2333\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
有人帮我理解上面的问题吗?以及如何解决?
提前致谢。
更新:
根据 Luctia 的建议,我解决了 cors 问题。但出现如下错误:
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)
需要为此添加任何解决方法吗?
您的浏览器似乎阻止了跨源读取 (CORB)。看看 this 文章。希望对您有所帮助!
您似乎在使用商店和效果进行测试。我假设效果会发出 http 请求,从而导致此问题。
这就是我鼓励模拟该服务的原因:
{
provide: GoogleBooksService,
useValue: { searchBooks: jest.fn() },
}
请参阅 NgRx Example App 示例。
我用 jestjs
为我的 ngrx/store
做测试。我得到了测试通过的结果。尽管出现如下错误:
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Cross origin http://localhost forbidden
at dispatchError (C:2333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:60:19)
at Object.validCORSHeaders (C:2333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:72:5)
at receiveResponse (C:2333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:842:21)
at Request.<anonymous> (C:2333\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:2333\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
有人帮我理解上面的问题吗?以及如何解决?
提前致谢。
更新: 根据 Luctia 的建议,我解决了 cors 问题。但出现如下错误:
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)
需要为此添加任何解决方法吗?
您的浏览器似乎阻止了跨源读取 (CORB)。看看 this 文章。希望对您有所帮助!
您似乎在使用商店和效果进行测试。我假设效果会发出 http 请求,从而导致此问题。
这就是我鼓励模拟该服务的原因:
{
provide: GoogleBooksService,
useValue: { searchBooks: jest.fn() },
}
请参阅 NgRx Example App 示例。