我在使用jest在Angular中执行测试时遇到了一个神秘的错误。
它读取
TypeError:将循环结构转换为JSON——比;用构造函数' object '从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();
});
});