- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Jasmine.js comparing arrays
Arrays can be compared in 2 ways −
They refer to the same array object in memory.
They may refer to different objects but their contents are all equal.
For case 1, jasmine provides the toBe method. This checks for reference. For example,
Example
describe("Array Equality", () => { it("should check for array reference equility", () => { let arr = [1, 2, 3]; let arr2 = arr // Runs successfully expect(arr).toBe(arr2); // Fails as references are not equal expect(arr).toBe([1, 2, 3]); }); });
Output
This will give the output −
Array Equality should check for array equility Message: Expected [ 1, 2, 3 ] to be [ 1, 2, 3 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().
For case 2 we can use the toEqual method and deep compare the arrays. For example,
Example
describe("Array Equality", () => { it("should check for array reference equility", () => { let arr = [1, 2, 3]; let arr2 = arr; // Runs successfully expect(arr).toEqual(arr2); // Runs successfully expect(arr).toEqual([1, 2, 3]); }); });
Output
This will give the output −
1 spec, 0 failures
Advertisements