jeudi 3 août 2017

Jasmine tests throw error with Decorators using the Injector manually in Angular 4+

We are using the injector from AppModule.injector in some of our Decorators. Everything works fine except for when we run tests that include such a Decorator at some point: Uncaught TypeError: enum_decorator_1.Enum is not a function while Enum() is the Decorator name.

export class AppModule {

    static injector: Injector;
    static changeDetectorRef: ChangeDetectorRef;

    constructor(public injector: Injector) {
        AppModule.injector = injector;
    }
}

I assume the bootstrapping in tests works differently and the injector isn't available for some reason. When I comment out the parts regarding the AppModule.injector in the Decorator, everything works.

The test code itself doens't matter. What happens is this:

  1. I initialize a Service fileService = new FileService(communicationServiceMock);
  2. That service imports an exported const variable
  3. That const variable imports a class that is decorated with Enum() decorator

Has anyone faced similar problems?

Aucun commentaire:

Enregistrer un commentaire