Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test freezes when expect(result).toBe(null) fails test. (Angular 2, Jasmine)

I have a test that checks if part DOM element has been removed by an ngIf. When I check the DOM using: fixture.debugElement.query(By.css(".button-area")) the result is either null or a DOM element.

If the result is null, then the following test works fine. But, if the test result contains an element, it doesn't simply fail the test, it freezes up the browser.

The test looks like this:

var result = fixture.debugElement.query(By.css(".button-area"))
expect(result).toBe(null)

I have also tried expect(result).toEqual(null) and .toBeFalsy() which have the same result.

What is the proper way to test if a DOM element has been removed properly?

UPDATE 1/23/2017

I have found out this issue is specifc to the element returned by:

fixture.debugElement.query(By.css(".button-area"))

This might be a bug with angular 2 or jasmine. If I use document.getElementByClassName("button-area") it is not an issue and the test works fine.

like image 755
ed-tester Avatar asked Jan 23 '17 16:01

ed-tester


People also ask

What is ComponentFixture Jasmine?

The ComponentFixture is a test harness for interacting with the created component and its corresponding element. Access the component instance through the fixture and confirm it exists with a Jasmine expectation: content_copy const component = fixture. componentInstance; expect(component).

What does detectChanges do in Angular Jasmine tests?

detectChanges() tells Angular to run change-detection. Finally! Every time it is called, it updates data bindings like ng-if, and re-renders the component based on the updated data. Calling this function will cause ngOnInit to run only the first time it is called.

What is TestBed in Jasmine?

TestBed is the primary api for writing unit tests for Angular applications and libraries.

What is Spec TS in Angular?

spec. ts file. This file contains unit tests for the main AppComponent. When running tests using the Angular CLI, all unit tests in files with the *. spec.


1 Answers

Since this seems to be a bug with Jasmine or Angular, here is a quick workaround if you are in a bind:

var result = fixture.debugElement.query(By.css(".button-area"))
expect(result === null).toBeTruthy()

You should expect to fix this in your own code when the issue is resolved.

like image 163
ed-tester Avatar answered Sep 21 '22 00:09

ed-tester