我在使用 jest 在 Angular 中执行测试时遇到了一个神秘的错误。
上面写着
类型错误:将循环结构转换为 JSON --> 从构造函数为“Object”的对象开始 | 属性“element”-> 具有构造函数“Object”的对象 | 属性 'componentProvider' -> 具有构造函数 'Object' 的对象 --- 属性 'parent' 在 stringify () 处闭合循环
或该错误的某些变体。
我找到了解决这个问题的方法,请看下面的答案。
看来这是某种内部玩笑错误。在阅读jestjs Github 页面上的类似问题后,我设法找到以下修复/解决方法:
npm run test:detectOpenHandles
或同等操作jest --detectOpenHandles
找到合成监听器@transform.start。请在您的应用程序中包含“BrowserAnimationsModule”或“NoopAnimationsModule”。
或者
找到合成属性@transitionMessages。请包括[...]
BrowserAnimationsModule
或NoopAnimationsModule
在您的测试中我不知道上述错误是由什么引起的。希望这有帮助。
编辑:这是我当前项目的 TestBed 配置示例:
import { TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { AppBarComponent } from './components/app-bar/app-bar.component';
import { SideNavComponent } from './components/side-nav/side-nav.component';
import { AngularMaterialModule } from './modules/angular-material.module';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule,
AngularMaterialModule,
BrowserAnimationsModule
],
declarations: [
AppComponent,
AppBarComponent,
SideNavComponent
],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8689 次 |
最近记录: |