Search code examples
angularangular9testbed

Angular9 debugElement.classes['my-class'] now returns undefined


I'm updating a repo that used to use Angular 8. In a test, using TestBed, I'm checking if a class exists using: fixture.debugElement.classes['my-class'].toBe(false)

After updating to Angular 9, this test no longer works, as the expression no longer returns false when the class doesn't exist, it returns undefined. I've fixed the test using: fixture.debugElement.classes['my-class'].toBeFalsy() by i'm afraid something else is failing silenty.

Checking Angular documentation, classes should only return a boolean: classes: { [key: string]: boolean; }

Is there any situation where classes will return undefined?


Solution

  • As the Ivy documentation says:

    DebugElement.classes returns undefined for classes that were added and then subsequently removed (previously, classes added and later removed would have a value of false).

    https://angular.io/guide/ivy-compatibility

    (I was having exatcly the same issue, and it broke a dozen of my unit tests as well)