Niels Ferguson
Niels Ferguson

Reputation: 372

Angular2 - Running all Unit Tests Fails Some Tests that Pass Individually

I am writing unit tests for my Angular project (Jasmine Karma).

All tests pass indidividually, but if I runn the whole suit, one test is failing with the following exception:

Uncaught TypeError: _this.handler.handle is not a function thrown

Skipping the test before the failing test, and all tests are passing again. So I'm guessing I need to clean up after each or some tests.

Is this correct? And if so, how do I do the actuall cleaning of a test. I can't seem to find anything on this online.

test before failing test

    describe('CreateBindingComponent', () => {
      let component: CreateBindingComponent;
      let fixture: ComponentFixture<CreateBindingComponent>;

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ CreateBindingComponent ],
          providers:[
            GetBindingEnumsService,
            GetBindingService,
            HttpClient,
            HttpHandler,
            CreateService,
          ],
          imports: [FormsModule, SelectDropDownModule, RouterTestingModule, MaterialModule]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(CreateBindingComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });

failing test

describe('DeleteDialogComponent', () => {
  let component: DeleteDialogComponent;
  let fixture: ComponentFixture<DeleteDialogComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ DeleteDialogComponent ],
      providers: [
        {provide : MatDialogRef, useValue : {}},
        {provide: MAT_DIALOG_DATA, useValue: {}},
        DeleteBindingService,
        HttpClient,
        HttpHandler
      ],
      imports: [MaterialModule]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DeleteDialogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

I'm new to Angular so please let me know if I need to provide information or code.

Thanks in advance for any help.

Upvotes: 3

Views: 3391

Answers (1)

learner
learner

Reputation: 255

I think Angular does not destroy listeners automatically to help you to get more details about your test execution. To remove it you simply use afterEach.

afterEach {
    fixture.destroy();
}

For More Details please check -> https://github.com/angular/angular/issues/18831

Upvotes: 2

Related Questions