Karma/Jasmine Angular InjectionToken Test fails unless I use fdescribe

160 Views Asked by At

I have the following spec test:

import { HttpClientTestingModule } from '@angular/common/http/testing';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { BASE_API_URL } from 'src/app/tokens/baseApiUrl.token';
import { RbacPermissionsService } from '../services/rbac-permissions.service';
import { SharedModule } from '../shared.module';

@Component({
  selector: 'app-mock-test',
  template: `<div *appHasPermission="{ items: 'view' }"></div>`,
  providers: []
})
export class MockTestComponent {
  constructor() {}
}

describe('HasPermissionDirective', () => {
  let mockTestComponent: MockTestComponent;
  let mockTestFixture: ComponentFixture<MockTestComponent>;
  let rbacPermissionsService: RbacPermissionsService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [MockTestComponent],
      imports: [SharedModule, HttpClientTestingModule],
      providers: [{provide: BASE_API_URL, useValue: '/some_api/'}]
    });

    rbacPermissionsService = TestBed.get(RbacPermissionsService);
    mockTestFixture = TestBed.createComponent(MockTestComponent);
    mockTestComponent = mockTestFixture.componentInstance;
  });

  it('should have no divs when permission is false', done => {
    spyOn(rbacPermissionsService, 'getPermission').and.returnValue(of(false));
    mockTestFixture.whenStable().then(() => {
      mockTestFixture.detectChanges();
      const divs = mockTestFixture.nativeElement.getElementsByTagName('div');
      expect(divs.length).toBeFalsy();
      done();
    });
  });

  it('should have a visible view element when permission is true', done => {
    spyOn(rbacPermissionsService, 'getPermission').and.returnValue(of(true));
    mockTestFixture.whenStable().then(() => {
      mockTestFixture.detectChanges();
      const divs = mockTestFixture.nativeElement.getElementsByTagName('div');
      expect(divs.length).toBeTruthy();
      done();
    });
  });
});

When I run this in conjunction with all other tests, it fails with the error:

NullInjectorError: StaticInjectorError(DynamicTestModule)[InjectionToken ]: 
  StaticInjectorError(Platform: core)[InjectionToken ]: 
    NullInjectorError: No provider for InjectionToken !

But when I run it with fdescribe the tests pass.

I wholeheartedly admit that spec tests are not my strong suit. My gut says their might be a timing issue since the tests work in isolation but not when run as part of the larger group of tests.

0

There are 0 best solutions below