question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

deepEqual arrays if their key changed as an object

See original GitHub issue

I recently write some test using AVA, something I have encountered like this below:

test('Array', t => {
  const arr1 = [1,2,3]
  const arr2 = arr1.slice()
  // normally we don't use array like this
  // but sometimes we might reach out of the array boundary unexpectedly
  for (let i = -1; i > -10; i--) {
    arr2[i] = 111
  }
  console.log(arr1, arr2)
  t.deepEqual(arr1, arr2)
})

The result is: image

I know the arr1 and arr2 has the same items and length as arrays, but array is also object. However, the keys of object changed unexpectedly might indicate that I have write something wrong. In that case, I got passed test and know nothing about it.

How I can compare two array also as an object? I can only figure out a workaround using two assertions like this (only deal with limited situations):

t.deepEqual(arr1, arr2)
t.deepEqual(Object.keys(arr1), Object.keys(arr2))

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
novemberborncommented, Aug 22, 2020

The fix is out in 5.0.1, thanks again @cagen.

2reactions
papbcommented, Aug 20, 2020

@cagen Please open an issue in the main AVA repository, I think this is a major issue 😮 nice catch!

Read more comments on GitHub >

github_iconTop Results From Across the Web

deepEqual arrays when their keys changed as an object #2563
I know the arr1 and arr2 has the same items and length as arrays, but array is also object. However, the keys of...
Read more >
Deep comparison of objects/arrays - javascript - Stack Overflow
If two objects all share the same basic properties, then they will all reference the same hidden classes, and JSON. stringify will work...
Read more >
5 Different Ways to Deep Compare JavaScript Objects
In this article, we will discuss five different ways to determine the equality of JavaScript objects. Two types of equalities in JavaScript.
Read more >
How to Compare 2 Objects in JavaScript | SamanthaMing.com
Objects are reference types so you can't use === or == to compare them. To check if 2 objects have the same key...
Read more >
How to check for array equality using javascript? - Flexiple
If x and y are both objects, it returns true if and only if they reference the same object. In Javascript, arrays are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found