使用jest预设角度执行单元测试,但收到一个警告,即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
我不知道如何运行节点--跟踪警告。看起来这是一个序列化问题,甚至只是一个警告,但不确定问题在哪里。有没有更好的方法可以找到异常
使用--detectOpenHandles
运行jest。这将向您展示您的测试规范到底出了什么问题。对我来说,缺少了Angular Material导入和服务模型。您可能会被提示添加BrowserAnimationsModule
,正如Nambi在回答时所暗示的那样
package.json:
"test": "jest --detectOpenHandles"
如果出现错误,通常会显示此消息,但该消息不会解析为标准异常。
正如@JamesBarret所提到的,使用笑话detect open handle
功能将为您提供一条很好的错误消息,告诉您该消息背后的真正问题是什么。
但另一条消息中提到的句柄--detectOpenHandles
已被弃用,并被--detect-open-handles
所取代。
您只需使用:(在cli上(
jest --detect-open-handles
或者在package.json文件中:
"test": "jest --detect-open-handles"
或者像我的情况一样,进行角度测试:
ng test --detect-open-handles
添加我的答案来帮助错过的人。就我而言,我错过了导入
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
只有在隔离运行其中一个测试用例时才能发现问题。
问题是异步操作,所以用下面的代码编写测试用例,为我解决问题
describe('DateSelectionComponent', () => {
let component: DateSelectionComponent;
let fixture: ComponentFixture<DateSelectionComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [DateSelectionComponent, SafePipe],
imports: [
SharedModule,
NgReduxTestingModule,
RouterTestingModule.withRoutes([])
],
providers: [
{
provide: PageAPIActions, useValue: { setPageState() { } }
}
]
}).compileComponents();
fixture = TestBed.createComponent(DateSelectionComponent);
component = fixture.componentInstance;
// Don't use Object.defineProperty when assigning value to class properties
component.page$ = of('value');
component.page = { destination: 'value' };
component.startDate = new NgbDate(2019, 2, 27);
component.minDate = new NgbDate(2019, 2, 27);
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
it('Should Match Snapshot', () => {
expect(fixture.debugElement.nativeElement).toMatchSnapshot()
});
});
在我的情况下,未将HttpClientModule
导入测试文件会触发神秘错误。更多详细信息请点击此处。
我在使用--watch
运行jest时遇到了同样的问题。删除--watch
停止了错误。
看起来错误是由于正在执行某些异步操作造成的。尝试将单元测试封装在waitForAsync函数中。