Not able to reset providers in beforeEach after creating TestBed once in beforeAll

3.9k Views Asked by At

We writing test case for a component which has many nested services and components (we cannot mock every service for our requirement). We have created module which contains all the components and services. In spec.ts file we have added module in TestBed, because of which beforeEach takes lot of time to create the environment. So we replaced beforeEach with beforeAll, but when we changing something in test case the same changes passed to next test case.

beforeAll(async(() => {
    TestBed.configureTestingModule({
      imports: [SharedModule]
    })
    .compileComponents();
    fixture = TestBed.createComponent(ChildComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));

  it('name should xyz', () => {
    component.name = 'xyz';
    expect(component.name).toEqual('xyz')
  });

    it('some other test case', () => {
      //I want component.name to be reset to default value
    expect(component.prop1).toBeTruthy();
  });
1

There are 1 best solutions below

0
hippeelee On

Does adding an afterEach help?

afterEach {
    fixture.destroy();
}

Jasmine docs on the setup / teardown functions are here: https://jasmine.github.io/2.1/introduction#section-Setup_and_Teardown