angular - 在 Jest 单元测试 Angular 中显示正确的错误

标签 angular jestjs cypress

我正在 NX angular 工作区中编写单元测试。有时它会给出这样的错误:

(node:15320) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'element' -> object with constructor 'Object'
    |     property 'componentProvider' -> object with constructor 'Object'
    --- property 'parent' closes the circle
    at stringify (<anonymous>)
    at writeChannelMessage (internal/child_process/serialization.js:117:20)
    at process.target._send (internal/child_process.js:779:17)
    at process.target.send (internal/child_process.js:677:19)
    at reportSuccess (C:\Users\INFINTY\angular\nfx__1-sep\node_modules\jest-runner\node_modules\jest-worker\build\workers\processChild.js:67:11)
(node:15320) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15320) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
它没有显示确切的错误。我知道 jasmine/karma 中有一个命令可以强制测试以显示正确的错误,但我忘记了。任何人都可以帮助我,我怎样才能在 jest/cypress 中得到确切的错误。

最佳答案

在我的应用程序中,这是导入 HttpClientModule 的问题。进入测试文件(在该组件中使用了 HttpClient):

import { HttpClientModule } from '@angular/common/http';

describe('AppComponent', () => {
    beforeEach(async () => {
        await TestBed.configureTestingModule({
            imports: [HttpClientModule, RouterTestingModule],
            declarations: [AppComponent],
        }).compileComponents();
    });
});
我正在使用:
  • Angular 10.1.4
  • 开 Jest 26.6.0
  • 开 Jest 预设 Angular 8.3.1
  • 关于angular - 在 Jest 单元测试 Angular 中显示正确的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63882290/

    相关文章:

    coded-ui-tests - cypress.io : contains() not waiting for element

    javascript - 相当于 cypress 中的 find() 的剧作家

    javascript - ReactTestUtils 中图像源的评估结果意外

    javascript - 如何清除每次测试之间的 Jest 状态( spy 、模拟)?

    Cucumber *.feature 和 cypress *.cy.ts 在同一个存储库中运行

    angular - 如何在父组件中测试 Angular 事件发射器?

    javascript - 如何实现文件上传的续传暂停功能?我正在使用 Node js、gridfs 和 multer

    angular - 如何在本地使用谷歌字体而不是在 Angular 4 项目中导入

    Angular 9 通用 - 运行错误 : can not read property 'type' of undefined - TokenStream

    javascript - 按照 jest docs 模拟 axios 但不工作