测试套件无法运行 TypeError: 将循环结构转换为 JSON

Bin*_*ode 10 jestjs angular

我在使用 jest 在 Angular 中执行测试时遇到了一个神秘的错误。

上面写着

类型错误:将循环结构转换为 JSON --> 从构造函数为“Object”的对象开始 | 属性“element”-> 具有构造函数“Object”的对象 | 属性 'componentProvider' -> 具有构造函数 'Object' 的对象 --- 属性 'parent' 在 stringify () 处闭合循环

或该错误的某些变体。

我找到了解决这个问题的方法,请看下面的答案。

Bin*_*ode 9

看来这是某种内部玩笑错误。在阅读jestjs Github 页面上的类似问题后,我设法找到以下修复/解决方法:

  1. 运行npm run test:detectOpenHandles或同等操作jest --detectOpenHandles
  2. 现在读取错误

找到合成监听器@transform.start。请在您的应用程序中包含“BrowserAnimationsModule”或“NoopAnimationsModule”。

或者

找到合成属性@transitionMessages。请包括[...]

  1. 要修复、导入BrowserAnimationsModuleNoopAnimationsModule在您的测试中

我不知道上述错误是由什么引起的。希望这有帮助。

编辑:这是我当前项目的 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)