4

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.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • Very much annoyed with the same issue now. For some reason, component mocking doesn't work for me. I will leave the link here for others https://stackoverflow.com/questions/41240163/mocking-child-components-angular-2 – Valera Tumash Nov 29 '22 at 01:38

1 Answers1

0

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.

Derrick Awuah
  • 373
  • 2
  • 5