What is the recommended approach to configure TestBed in Jasmine test cases in Angular?

141 Views Asked by At

I have started writing test cases for Angular and while reading articles from internet I found there are different ways in which we can configure our TestBed. Below are few examples:

Example 1:

beforeEach(async(() => {
  TestBed.configureTestingModule({
   ...
  }).compileComponents()
    .then(() => {
      fixture = TestBed.createComponent(Component);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
}));

Example 2:

beforeEach(async(() => {
  TestBed.configureTestingModule({
   ...
  }).compileComponents();
}));
beforeEach(() => {
  fixture = TestBed.createComponent(Component);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

Example 3:

beforeEach(async(() => {
  TestBed.configureTestingModule({
   ...
  }).compileComponents();
  fixture = TestBed.createComponent(Component);
  component = fixture.componentInstance;
  fixture.detectChanges();
}));

Example 4:

beforeEach(async() => {
  TestBed.configureTestingModule({
   ...
  });
  await TestBed.compileComponents();
  fixture = TestBed.createComponent(Component);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

I need help to understand which is the recommended approach to follow?

1

There are 1 best solutions below

0
On

If all of them work, you can pick your favorite. I personally use Example 2 since that's how I see it being done most places.