debugging Karma:如何调试RangeError:是否超出最大调用堆栈大小?

8yoxcaq7  于 2022-11-14  发布在  其他
关注(0)|答案(2)|浏览(150)

我正在开发一个Angular应用程序,并在我的单元测试中使用Jasmine和Karma。在我的一个组件规范中,我开始得到错误:Uncaught RangeError: Maximum call stack size exceeded .
我很难找到/理解这个堆栈溢出的原因,我很高兴有一个结构化的方法来调试这个错误。
有问题的组件(...component.spec.ts)的代码由下式给出:

import {
  ComponentFixture,
  TestBed,
  fakeAsync,
  tick
} from "@angular/core/testing";
import "hammerjs";

import { RegisterStudentComponent } from "./register-student.component";

import { MaterialModule } from "../../modules/material/material.module";
import { RouterTestingModule } from "@angular/router/testing";
import { ReactiveFormsModule } from "@angular/forms";
import { UserService } from "src/app/core/user.service";
import { userServiceStub } from "src/testing/user-service-stub";
import { LoadingComponent } from "src/app/loading/loading.component";
import { Location } from "@angular/common";
import { Routes } from "@angular/router";

const routes: Routes = [
  {
    path: "schüler",
    component: RegisterStudentComponent
  }
];

describe("RegisterStudentComponent", () => {
  let component: RegisterStudentComponent;
  let fixture: ComponentFixture<RegisterStudentComponent>;
  let registerStudent: HTMLElement;
  let location: Location;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [RegisterStudentComponent, LoadingComponent],
      imports: [
        MaterialModule,
        RouterTestingModule.withRoutes(routes),
        ReactiveFormsModule
      ],
      providers: [{ provide: UserService, useValue: userServiceStub }]
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(RegisterStudentComponent);
    component = fixture.componentInstance;
    registerStudent = fixture.nativeElement;

    fixture.detectChanges();

    location = TestBed.get(Location);
  });

  const setupValidForm = () => {
    component.subjects = ["Physics"];
    component.selectedSubjects = ["Physics"];
    component.form.setValue({
      agb: true,
      email: "test@test.de",
      hourlyRate: 5,
      password: "test",
      name: "test"
    });
  };

  it("should create", () => {
    expect(component).toBeTruthy();
  });

  it("should add subject if click on subject button", () => {
    component.subjects = ["Physics"];
    fixture.detectChanges();

    let physicsButton = registerStudent.querySelector("div[class=subjects]")
      .children[0];
    physicsButton.dispatchEvent(new Event("click"));

    expect(component.selectedSubjects.includes("Physics")).toBeTruthy();
  });

  it("should remove subject if selected subject is clicked", () => {
    component.subjects = ["Physics"];
    component.selectedSubjects = ["Physics"];
    fixture.detectChanges();

    let physicsButton = registerStudent.querySelector("div[class=subjects]")
      .children[0];
    physicsButton.dispatchEvent(new Event("click"));

    expect(component.selectedSubjects).toEqual([]);
  });

  it("should be initialized invalid", () => {
    expect(component.isValid()).not.toBeTruthy();
  });

  it("should be valid if filled out", () => {
    setupValidForm();
    fixture.detectChanges();

    expect(component.isValid()).toBeTruthy();
  });

  it("should be invalid if no subject is selected", () => {
    setupValidForm();
    component.selectedSubjects = [];
    fixture.detectChanges();
    expect(component.isValid()).not.toBeTruthy();
  });

  it("should not be valid if email is not valid", () => {
    setupValidForm();
    component.form.setValue({ ...component.form.value, email: "test" });
    fixture.detectChanges();
    expect(component.isValid()).not.toBeTruthy();
  });

  it("should not be valid if email is empty", () => {
    setupValidForm();
    component.form.setValue({ ...component.form.value, email: "" });
    fixture.detectChanges();
    expect(component.isValid()).not.toBeTruthy();
  });

  it("should not be valid if password is empty", () => {
    setupValidForm();
    component.form.setValue({ ...component.form.value, password: "" });
    fixture.detectChanges();
    expect(component.isValid()).not.toBeTruthy();
  });

  it("should not be valid if agbs are not checked", () => {
    setupValidForm();
    component.form.setValue({ ...component.form.value, agb: false });
    fixture.detectChanges();
    expect(component.isValid()).not.toBeTruthy();
  });

  // it("should load until user registration is complete", fakeAsync(() => {
  //   setupValidForm();
  //   let submitButton = registerStudent.querySelector<HTMLButtonElement>(
  //     "main > button"
  //   );
  //   submitButton.click();
  //   tick();
  //   expect(component.isLoading).toBeTruthy();
  // }));

  it("should redirect to student profile after click on registration button", fakeAsync(() => {
    setupValidForm();
    let submitButton = registerStudent.querySelector<HTMLButtonElement>(
      "main > button"
    );
    submitButton.click();
    tick();
    expect(location.path()).toBe("/sch%C3%BCler");
  }));
});
w51jfk4q

w51jfk4q1#

当您使用一个函数调用另一个函数,而另一个函数又调用另一个函数时,就会发生这种情况,依此类推。
我会尝试通过运行每个时间组的测试来隔离问题。对于运行单个测试:使用fit而不是it

fit('example', function () {
    // 
});

忽略特定测试:使用xit

xit('example', function () {
        // 
    });

则它将忽略该特定测试。
我希望它能帮助你找出问题所在。

nhaq1z21

nhaq1z212#

我也面临着类似问题:只需停止测试服务器并再次运行它。这解决了我的问题。

相关问题