Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected [ ] to be [ ] Jasmine, how to check empty array

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?

like image 429
Kailas Avatar asked Oct 03 '17 11:10

Kailas


People also ask

How do you check if 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.

Is empty [] is true?

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.

How do you know if an oversized array is empty?

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.

How to define an empty array in JavaScript?

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.

What is the difference between elementarrayfinder and Jasmine-matchers?

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.

How to check if an array is empty if array is null?

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.

Why can't I compare two empty objects/arrays?

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.


1 Answers

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.

like image 100
Evan Trimboli Avatar answered Sep 28 '22 11:09

Evan Trimboli