TheCoderGuy
TheCoderGuy

Reputation: 883

Unit Testing in Angular not.toBe(true) vs toBeFalsy()

I am writing a Angular Unit Testing which I am facing with an error. If I write toBeFalsy() the test will fail. But If I write not.toBe(true) the test will work.

May someone help me.

let fixture: ComponentFixture<RootComponent>;
let debugElement: DebugElement;

beforeEach(async () => {
    fixture = TestBed.createComponent(RootComponent);
    await executeChangeDetection(fixture);
    component = fixture.componentInstance;
    debugElement = fixture.debugElement.query(By.css('#overlay'));
  });


it('should hide overlay and reset timeout', () => {
    const appState = new ApplicationStateService(null as any);
    appState.timeout = false;
    component.isTimeoutOverlayVisible = appState.timeout;
    fixture.detectChanges();
    expect(debugElement.nativeElement).toBeFalsy();
  });

toBeFalsy() the test will fail but as message will come that.
Expected <overlay id="overlay">...</overlay> to be falsy.

not.toBe(true) the test will work.

Upvotes: 2

Views: 5002

Answers (1)

KLTR
KLTR

Reputation: 1344

In javascript there are trues and truthys. When something is true it is obviously true or false. When something is truthy it may or may not be a boolean, but the "cast" value of is a boolean.

so in your case, toBeFalsy means that the native element does not exist or is equals to null/undefined.

the not.toBe(true) checks wheter the nativeElement !== true which also checks for type and value, and of course the native element value is not 'true' as a boolean type.

for more information check out: toBe(true) vs toBeTruthy() vs toBeTrue()

Upvotes: 3

Related Questions