Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find name 'async' when testing Angular 11 with jest

I'm trying to write Angular test using jest :

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

When i launched :

npm run test:watch 

I got this error :

error TS2304: C annot find name 'async'.

like image 749
Khaled Boussoffara Avatar asked Oct 25 '25 05:10

Khaled Boussoffara


2 Answers

You have to change async to waitForAsync in Angular 11 because they changed it. They changed it because the async you import from @angular/core/testing in previous versions is similar to the native async of JavaScript and could cause confusion.

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

Link for waitForAsync.

like image 74
AliF50 Avatar answered Oct 26 '25 20:10

AliF50


async is a not a function. Try it like this:

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

I'm not to sure if you would even need to mark the function as async because you don't use the await inside your before each.

Have a look at the documentation

like image 33
Erbsenkoenig Avatar answered Oct 26 '25 20:10

Erbsenkoenig