Getting error while trying to check for empty array. I tried using:
Case 1: By initializing as an array
expect(fixture.componentInstance.dataSource).toBe([]);
Case 2: By initializing as an array
let expectedAry = new Array; expect(fixture.componentInstance.dataSource).toBe(expectedAry);
Both the case have the same error:
Expected [ ] to be [ ].
Arrays can also be checked by their length, the following works fine
expect(fixture.componentInstance.dataSource.length).toEqual(0);
0 length is an option, but not sure if that is the right way to check whether an array is empty. Do we have a better option for checking whether an array is empty?
To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.
As we know, empty string, '' is a falsy value, hence transforming an empty string to number will return us a value 0 . In fourth iteration, the first condition is satisfied, where the types are equal and the numbers are equal. Henceforth, the final value of the [] == false reduces to 0 == 0 which is true.
To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null. The array is empty.
There is no standard definition to define an empty array. We will assume an array is empty if Array is null. Array has no elements inside it. All the elements inside the array are null. To check if an array is null, use equal to operator and check if array is equal to the value null.
the answer is four lines down in the protractor src. ElementArrayFinder extends Promise, while jasmine-matchers checks first checks that errors is an actual array exactly how Array.isArray is done , which will return false; This is also consistent with expect (scope.page.errors.length).toBe (0) being undefined because promises do not have lengths.
We will assume an array is empty if Array is null. Array has no elements inside it. All the elements inside the array are null. To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null.
For some reason empty objects/arrays aren't expected to be empty objects or arrays. This is the expected behavior. toBe is an identity comparison, IE, is it the same object. You are comparing two different objects/arrays. You want an equality comparison, which you can get by using toEqual.
toBe
doesn't check the contents of the array, it only checks if the references are the same.
expect([1]).toBe([1])
will fail because the references are different.
You should use toEqual
, which has some smarts to check the array contents as opposed to just doing a reference comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With