在开玩笑地测试 Angular 11 时找不到名称"异步"



我正在尝试使用jest编写Angular测试:

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [BookListComponent],
}).compileComponents();
}));

当我启动时:

npm run test:watch 

我收到此错误:

错误 TS2304:C 找不到名称"异步"。

您必须在 Angular 11 中将async更改为waitForAsync,因为他们更改了它。他们更改了它,因为您从以前版本中的@angular/core/testing导入async类似于 JavaScript 的本机async,可能会导致混淆。

import { waitForAsync } from '@angular/core/testing';
.....
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [BookListComponent],
}).compileComponents();
}));

等待异步的链接。

async

不是一个函数。像这样尝试:

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [BookListComponent],
}).compileComponents();
});

我不确定您是否甚至需要将函数标记为异步,因为您不会在每个函数之前使用await。

查看文档

最新更新