UnhandledPromiseRejectionWarning: TypeError: 使用 Jest + Angular 将循环结构转换为 JSON

San*_*isy 55 javascript node.js typescript jestjs angular

使用 jest-preset-angular 执行单元测试,但收到警告,因为UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON不确定导致错误的原因,因此应用程序卡住并且未运行其他单元测试。

PASS  src/app/pages/result/result-filter/result-filter.component.spec.ts (6.251 s)
 PASS  src/app/pages/result/search-navigation/search-navigation.component.spec.ts
 PASS  src/app/pages/result/filter-modal/filter-modal.component.spec.ts (5.699 s)
 PASS  src/app/app.component.spec.ts
 PASS  src/app/pages/test-type/test-type.component.spec.ts (12.857 s)
(node:3280) 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:808:17)
    at process.target.send (internal/child_process.js:706:19)
    at reportSuccess (/Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp/node_modules/jest-worker/build/workers/processChild.js:67:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3280) 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:3280) [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.
(node:3281) 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:808:17)
    at process.target.send (internal/child_process.js:706:19)
    at reportSuccess (/Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp/node_modules/jest-worker/build/workers/processChild.js:67:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3281) 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:3281) [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.
(node:3279) 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:808:17)
    at process.target.send (internal/child_process.js:706:19)
    at reportSuccess (/Users/macbook/Projects/Playtime Projects/IDP/Idp.Bx.Ui/idp/node_modules/jest-worker/build/workers/processChild.js:67:11)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3279) 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:3279) [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.

 RUNS  src/app/pages/location/location.component.spec.ts
 RUNS  src/app/pages/signup/signup.component.spec.ts
 RUNS  src/app/pages/login/login.component.spec.ts

Test Suites: 10 passed, 10 of 20 total
Tests:       16 passed, 16 total
Snapshots:   1 obsolete, 5 passed, 5 total
Time:        2180 s
Run Code Online (Sandbox Code Playgroud)

我不确定如何运行节点 --trace-warnings。看起来这是一个序列化问题,即使它只是一个警告,但不确定问题出在哪里。有没有更好的方法来查找异常

Jam*_*ett 73

Run jest with --detectOpenHandles. This will show you what is actually wrong with your test spec. For me, there were missing Angular Material imports and service mocks. You may be prompted to add the BrowserAnimationsModule, as Nambi alluded to in his answer

package.json:

"test": "jest --detectOpenHandles"
Run Code Online (Sandbox Code Playgroud)

  • 救星!我遇到了真正的运行时错误,这帮助我识别缺少的依赖项 (2认同)

Sim*_*sch 8

此消息通常会出现,如果出现错误,则不会解析为标准异常。

正如@JamesBarret 所提到的,使用 jestsdetect open handle功能将为您提供一条很好的错误消息,告诉您此消息背后的真正问题是什么。

--detectOpenHandles另一条消息中提到的句柄已被弃用,并被替换为--detect-open-handles.

您可以使用:(在您的 cli 上)

jest --detect-open-handles
Run Code Online (Sandbox Code Playgroud)

或者在 package.json 文件中:

"test": "jest --detect-open-handles"
Run Code Online (Sandbox Code Playgroud)

或者在我的情况下进行角度测试:

ng test --detect-open-handles
Run Code Online (Sandbox Code Playgroud)


小智 -2

看起来错误是由于正在进行一些异步操作造成的。尝试将单元测试包装在 waitForAsync 函数中。