jest错误TS18047“对象可能为空”

g52tjvyc  于 2023-06-20  发布在  Jest
关注(0)|答案(1)|浏览(317)

我正在为一个angular组件编写一个测试(jtest),但由于这个错误而失败。有什么想法吗?:)

it("should require valid email", () => {
    spectator.component.email.setValue({ email: "invalidemail" });
    expect(spectator.component.email).toEqual(false);
});

从我的.ts文件:

get email(): AbstractControl | null {
        return this.form.get("email");
    }

我的错误:
enter image description here
我试过这个spectator.component.email(“invalidemail”);

rkue9o1l

rkue9o1l1#

这个错误告诉你this.form是undefined。所以你必须先初始化它:

it("should require valid email", () => {
    spectator.component.form = new FormGroup({email: new FormControl()});
    spectator.component.email.setValue({ email: "invalidemail" });
    expect(spectator.component.email).toEqual(false);
});

相关问题