Testing UpgradeComponent throws NullInjectorError: No provider for $injector! error

Viewed 346

I have a angularjs component that I am upgrading:

import { Directive, ElementRef, Injector, Output, EventEmitter } from '@angular/core';
import { UpgradeComponent } from '@angular/upgrade/static';

@Directive({
  selector: 'up'
})
export class UpDirective extends UpgradeComponent {
  constructor(elementRef: ElementRef, injector: Injector) {
    super('old', elementRef, injector);
  }
}

This component works just fine when I run the application.

However when I try and test the Angular 8 component that uses that directive I get the following error:

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

I assume that is coming from the injector that is used in the UpgradeComponent. However I am not sure how to provide that to my spec.ts.

Angular does a good job documenting how to make this work in an application, but nothing on how to test: https://angular.io/api/upgrade/static/UpgradeComponent

Also looked here: https://github.com/angular/angular/issues/24369, however still not clear how to test.

1 Answers

Here is a good example of how you can setup tests for components using it. Hard to be more helpful without a stackblitz, but when I had to test something similar this worked for me.

When I did it I just took that stuff outside of the "it" so it could be used as a setup. Pretty sure I did most of that setup in the beforeEach. Just had the variables I needed declared in the describe but initialized in the beforeEach so they could be used in the tests.

Hard to be more helpful without a stackblitz.

Related